On the Optimization of if-else in Java

problem description

this is a relatively simple topic that can be written. What I want to ask is whether these if-else can be optimized

.

related codes

/ / Please paste the code text below (do not replace the code with pictures)

public static void main (String [] args) {

    Scanner input = new Scanner(System.in);
    System.out.println("Enter the number of students:");
    int n = input.nextInt();
    int[] score = new int[n];
    System.out.println("Enter scores:");

    int max = 0;
    for (int i = 0; i < n; iPP) {
        score[i] = input.nextInt();
        if (score[i] > max) {
            max = score[i];
        }
    }

    for (int i = 0; i < n; iPP) {
        if (score[i] >= max - 10)
            System.out.println("Student " + i + " score is " + score[i] + " and grade is A");
        else if (score[i] >= max - 20 && score[i] < max - 10)
            System.out.println("Student " + i + " score is " + score[i] + " and grade is B");
        else if (score[i] >= max - 30 && score[i] < max - 20)
            System.out.println("Student " + i + " score is " + score[i] + " and grade is C");
        else if (score[i] >= max - 40 && score[i] < max - 30)
            System.out.println("Student " + i + " score is " + score[i] + " and grade is D");
        else
            System.out.println("Student " + i + " score is " + score[i] + " and grade is F");
    }


}
Jun.09,2022

refer to the following code

  https://stackoverflow.com/que...

Menu