Home>

New to java

I would like to know what happens if you make this issue all documents in java

Enter the test score and create a program that displays the grade (excellent, good, good, bad)

Grades are decided according to the following rules according to the score
However, the following method getGrade is defined, and the main method displays the grade according to the grade returned from this method.

90 points or better
Greater than 80 and less than 90
70 points or more and less than 80 points Good
60 points or more and less than 70 points
No less than 60 points

int getGrade(int score)
The grade value corresponding to the test score specified in the dummy argument is returned as the return value. The correspondence between the score and grade is as follows.

Score grade
90 points or more 4
80 points or more and less than 90 points 3
70 points or more and less than 80 points 2
60 points or more and less than 70 points 1
Less than 60 points 0

[Execution example]
Please enter the test score 77
Good

  • Answer # 1

    For a question that was ten months ago, the questioner wouldn't see it anymore.

    class Test {
        static int getGrade(int x) {return x/60*(x/10-5)-x/100;}
        public static void main(String[]args) {
            System.out.print("Please enter the test score");
            System.out.println(new String[]{"No", "Yes", "good", "excellent", "excellent"}
                [getGrade(new java.util.Scanner(System.in).nextInt())]);
        }
    }


    Please do not enter more than 110 points.

  • Answer # 2

    import java.util.Scanner;
    public class Q216581 {
            public static String[] GRADES = {"No", "Yes", "Good", "Excellent", "Excellent" };
            public static void main(String[] args) throws Exception {
                    try (var sc = new Scanner(System.in)) {
                            System.out.print("Please enter the test score");
                            System.out.flush();
                            System.out.println(GRADES[getGrade(sc.nextInt())]);
                    }
            }
            public static int getGrade(int score) {
                    return Math.min(Math.max((score/10)-5, 0), 4);
            }
    }