1.if-else 在else子句嵌套
import java.util.Scanner;
public class Gif {
public static void main(String[] args) {
System.out.println("请输入成绩:");
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
char grade=0;
if(score<60){
grade='E';
}else if(score<70){
grade='D';
}else if(score<80){
grade='C';
} else if (score<90) {
grade='B';
} else if (score<=100) {
grade='A';
}
System.out.println(grade);
}
}
2.在if子句嵌套
import java.util.Scanner;
public class Gif {
public static void main(String[] args) {
System.out.println("请输入成绩:");
Scanner scanner = new Scanner(System.in);
int score = scanner.nextInt();
char grade=0;
if(score>=60){ if(score>=70){
if(score>=80){
if(score>=90){
grade='A';
}else{
grade='B';
}
}else{
grade='C';
}
}else{
grade='D';
}
}else{
grade='E';
}
System.out.println(grade);
}
}
3.switch
import java.util.Scanner;
public class Gswitch {
public static void main(String[] args) {
System.out.println("请输入成绩:");
Scanner scanner=new Scanner(System.in);
int score= scanner.nextInt();
char grade;
switch (score/10){
case 10:
case 9:grade='A';
break;
case 8:grade='B';
break;
case 7:grade='C';
break;
case 6:grade='D';
break;
default:grade='E';
break;
}
System.out.println("等级是:"+grade);
}
}