jmu-Java-02基本语法-06-枚举 (3分)

【jmu-Java-02基本语法-06-枚举 (3分)】7-4 jmu-Java-02基本语法-06-枚举 (3分)
定义一个枚举类型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="https://www.it610.com/article/+grade); }

输入样例:
90 80 70 60

输出样例:
Excellent class name=class Grade grade value=https://www.it610.com/article/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

import java.util.Scanner;
enum Grade{
A,B,C,D,E;
}
public class Main {

public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
while(sc.hasNext()) {
int score = sc.nextInt();
printGradeInfo(getGrade(score));
}
sc.close();
}
public static Grade getGrade(int score) {
Grade g ;
switch((int)score/10) {
case 10:
case 9:
System.out.println("Excellent");
g=Grade.A;
break;
case 8:
System.out.println("Good");
g=Grade.B;
break;
case 7:
System.out.println("Average");
g=Grade.C;
break;
case 6:
System.out.println("Fair");
g=Grade.D;
break;
default:
System.out.println("Poor");
g=Grade.E;
}
return g;
}
public static void printGradeInfo(Grade grade){
System.out.println("class name="+Grade.class);
System.out.println("grade value="https://www.it610.com/article/+grade);
}

}

    推荐阅读