switch语句允许针对值列表对变量进行相等性测试。每个值称为一个案例,并针对每种情况检查要打开的变量。
public class Test { public static void main(String args[]) { //字符等级= args [0] .charAt; char grade = 'C'; switch(grade) { case 'A' : System.out.println("Excellent!"); break; case 'B' : case 'C' : System.out.println("Well done"); break; case 'D' : System.out.println("You passed"); case 'F' : System.out.println("Better try again"); break; default : System.out.println("Invalid grade"); } System.out.println("Your grade is " + grade); } }
输出结果
Well done Your grade is C