Java包含两种分支语句:if和switch
if:一路分支
if...else:两路分支
if...else if:多路分支
switch:多路分支
/*
* &:非短路与,与两端的表达式都需要执行
* &&:短路与,若左边表达式为false,则右边表达式不执行;全表达式为false
* |:非短路或
* ||:短路或
*/
//闰年判断,if实现
Scanner scan = new Scanner(System.in);
int year = scan.nextInt();
if(year%4==0 && year%100!=0 || year%400==0){
System.out.println(year + "是闰年");
else{
System.out.println(year + "不是闰年");
}
//成绩等级判断,switch实现
Scanner scan = new Scanner(System.in);
System.out.print("请输入成绩(0-150):");
int score = scan.nextInt();
//判断成绩范围
if(score < 0 || score > 150){
System.out.println("成绩不合理");
}else if(score >= 135){
System.out.println("A");
}else if(score >= 120){
System.out.println("B");
}else if(score >= 90){
System.out.println("C");
}else{
System.out.println("D");
}