用if语句进行判断
01 代码
import java.util.Scanner;
public class Demo01IfElse{
public static void main(String[] args){
Scanner sc = new Scanner(System.in);
System.out.println("请输入成绩,按回车结束");
int score = sc.nextInt();
if(90<score && score<=100){
System.out.println("A");
}else if(80<score && score<=90){
System.out.println("B");
}else if(70<score && score<=80){
System.out.println("C");
}else if(60<score && score<=70){
System.out.println("D");
}else if(0<=score && score<=60){
System.out.println("不及格");
}else{
System.out.println("请输入0到100的有效数字");
}
}
}
02 测试结果
D:\>javac Demo01IfElse.java
D:\>java Demo01IfElse
请输入成绩,按回车结束
95
A
D:\>javac Demo01IfElse.java
D:\>java Demo01IfElse
请输入成绩,按回车结束
85
B
D:\>javac Demo01IfElse.java
D:\>java Demo01IfElse
请输入成绩,按回车结束
75
C
D:\>javac Demo01IfElse.java
D:\>java Demo01IfElse
请输入成绩,按回车结束
65
D
D:\>javac Demo01IfElse.java
D:\>java Demo01IfElse
请输入成绩,按回车结束
55
不及格
D:\>javac Demo01IfElse.java
D:\>java Demo01IfElse
请输入成绩,按回车结束
1000
请输入0到100的有效数字
D:\>javac Demo01IfElse.java
D:\>java Demo01IfElse
请输入成绩,按回车结束
-1
请输入0到100的有效数字
D:\>