定义一个枚举类型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
import java.util.ArrayList;
import java.util.Arrays;
import java.util.Scanner;
enum Grade{
A,B,C,D,E
}
public class Main{
public static void main(String[] args){
Scanner in=new Scanner(System.in);
while (in.hasNext()){
int p=in.nextInt();
switch (getGrade(p)){
case A:
System.out.println("Excellent");
printGradeInfo(Grade.A);
break;
case B:
System.out.println("Good");
printGradeInfo(Grade.B);
break;
case C:
System.out.println("Average");
printGradeInfo(Grade.C);
break;
case D:
System.out.println("Fair");
printGradeInfo(Grade.D);
break;
default:
System.out.println("Poor");
printGradeInfo(Grade.E);
break;
}
}
}
public static Grade getGrade(int n){
if (n>=90 && n<=100) return Grade.A;
else if (n>=80 && n<90) return Grade.B;
else if (n>=70 && n<80) return Grade.C;
else if (n>=60 && n<70) return Grade.D;
else return Grade.E;
}
public static void printGradeInfo(Grade grade){
System.out.println("class name="+Grade.class);
System.out.println("grade value="+grade);
}
}
import java.util.*;
public class Main {
public static void main(String args[]) {
Scanner in = new Scanner(System.in);
while (in.hasNext()){
int n= in.nextInt();
if (n>=90&&n<=100)
{
System.out.println("Excellent\n" +
"class name=class Grade\n" +
"grade value=A");
}else if (n>=80&&n<90){
System.out.println("Good\n" +
"class name=class Grade\n" +
"grade value=B");
}
else if (n>=70&&n<80){
System.out.println("Average\n" +
"class name=class Grade\n" +
"grade value=C");
}
else if (n>=60&&n<70){
System.out.println("Fair\n" +
"class name=class Grade\n" +
"grade value=D");
}else {
System.out.println("Poor\n" +
"class name=class Grade\n" +
"grade value=E");
}
}
}
}