switch(expression){ case value: //语句 break;//可选 case value: //语句 break;//可选 default://可选 //语句 }
package JAVASE.struct; import java.io.InputStream; public class switch01 { public static void main(String[] args) { //switch是匹配一个具体的值 case:穿透 char grade = 'B'; switch (grade) { case 'A': System.out.println("优秀"); break; case 'B': System.out.println("良好"); break; case 'C': System.out.println("及格"); break; } } }
package JAVASE.struct; import java.util.Scanner; public class switch02 { public static void main(String[] args) { Scanner scanner = new Scanner(System.in); System.out.println("请输入成绩(A,B,C):"); String grade = scanner.nextLine(); switch (grade){ case "A": System.out.println("优秀"); break; case "B": System.out.println("良好"); break; case "C": System.out.println("及格"); break; } } }
2021.8.5