1. 程式人生 > 其它 >if多選擇結構與switch

if多選擇結構與switch

3的值為true執行程式碼

}else{

//如果以上布林表示式都不為true執行程式碼

}

public static void main(String[] args) {
//考試分數大於60就是及格,小於60就不及格
Scanner scanner = new Scanner(System.in);

System.out.println("請輸入成績");
double score = scanner.nextDouble();

if (score==100){
System.out.println("恭喜滿分");
}else if (score>=90&&score<100){
System.out.println("A級");
}else if (score>=80&&score<90){
System.out.println("B級");
}else if (score>=70&&score<80){
System.out.println("C級");
}else if (score>=60&&score<70){
System.out.println("D級");
}else if (score>100||score<0){
System.out.println("成績有誤");
}else{
System.out.println("不及格");

}

scanner.close();
}

switch

case穿透:如果沒有default程式會一直往下執行

public static void main(String[] args) {
//case穿透
char grade = 'C';

switch (grade){
case 'A':
System.out.println("優秀");
case 'B':
System.out.println("良好");
case 'C':
System.out.println("及格");
case 'D':
System.out.println("再接再厲");
default:
System.out.println("未知等級");


}
}


public static void main(String[] args) {
//case穿透
char grade = 'C';

switch (grade){
case 'A':
System.out.println("優秀");
break;
case 'B':
System.out.println("良好");
break;
case 'C':
System.out.println("及格");
break;
case 'D':
System.out.println("再接再厲");
break;
default:
System.out.println("未知等級");


}
}