学习Switch语句。
Java流程控制
Switch选择结构
switch语句对流程进行控制。switch作为一个开关,当变量表达式的值对应case中的值时,执行case后面的语句后通过Break跳出switch语句。
例如对成绩等级进行判断。
static char level;
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
System.out.println("请输入分数:");
int score = scanner.nextInt();
if (score <=100 && score >= 80){
level = 'A';
}
else if (score <80 && score >= 70) {
level = 'B';
}
else if (score <70 && score >= 60){
level = 'C';
}
else level = 'D';
switch (level){
case 'A':
System.out.println("优秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
case 'D':
System.out.println("不及格");
}
}

浙公网安备 33010602011771号