定义一个枚举类型Grade来表示考试的4个等级,值包含A,B,C,D,E
编写一个函数Grade getGrade(int score)
将传递进来的score转化为枚举类型
, >=90 and <=100返回A,
,>=80 and <90 返回B,
,>=70 and <80 返回C,
,>=60 and <70返回D, 其他的返回E
#main方法
输入分数后,调用getGrade函数返回相应的Grade,使用switch根据Grade,根据分数打印不同的评价:
Excellent Good Average Fair Poor
并且每输出一个评语后,要调用如下函数
public static void printGradeInfo(Grade grade){
System.out.println("class name="+Grade.class);
System.out.println("grade value="+grade);
}
输入样例:
90
80
70
60
输出样例:
Excellent
class name=class Grade
grade value=A
Good
class name=class Grade
grade value=B
Average
class name=class Grade
grade value=C
Fair
class name=class Grade
grade value=D
/*定义一个枚举类型Grade来表示考试的4个等级,值包含A,B,C,D,E*/
import java.util.*;
enum Grade {
A,B,C,D,E;
}
public class Main{
public static void main(String[] args){
Scanner in = new Scanner(System.in);
int score;
Grade t;
while(in.hasNext()){
score = in.nextInt();
t=getGrade(score);
switch(t){
case A:System.out.println("Excellent");break;
case B:System.out.println("Good");break;
case C:System.out.println("Average");break;
case D:System.out.println("Fair");break;
case E:System.out.println("Poor");break;
}
printGradeInfo(t);
}
}
public static Grade getGrade(int score){
if(score>=90 && score<=100)return Grade.A;
else if(score>=80 && score<90)return Grade.B;
else if(score>=70 && score<80)return Grade.C;
else if(score>=60 && score<70)return Grade.D;
return Grade.E;
}
public static void printGradeInfo(Grade grade){
System.out.println("class name="+Grade.class);
System.out.println("grade value="+grade);
}
}