7-7 jmu-Java-02基本语法-06-枚举 --笔记篇

2024-01-07 18:54:49

题目

定义一个枚举类型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);
    }
}

笔记

  1. 当然简单地也可以单纯if else
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");
            }
        }


    }
}

文章来源:https://blog.csdn.net/weixin_52057528/article/details/135430078
本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。