I have a Java assignment to make a grading scale like the one below.

 import java.util.Scanner;
public class gradeSelection {
   public static void main(String[] args){
   Scanner input = new Scanner(System.in);
   System.out.print("Enter Score: ");
   double score = input.nextDouble();
   if (score >= 90) {
        System.out.println("Your score is " + score + " which is an A");
        if ((score < 90) && (score >= 80)){
            System.out.println("Your score is " + score + " which is an B");
            if ((score < 80) && (score >= 70)){
                System.out.println("Your score is " + score + " which is an C");
                if ((score < 70) && (score >= 60)){
                    System.out.println("Your score is " + score + " which is an D");
                    if (score < 60){
                        System.out.println("Your score is " + score + " which is an E");
                    }//endif
                }//endif    
            }//endif
        }//endif
    }//endif
}
}

I can get it to work in Eclipse but it will terminate after I input a number. What am I doing wrong exactly?

有帮助吗?

解决方案

You have to use if and else if, because if the first condition doesn't match it will exit and will not reach the nested (the other) conditions:

Try to change it like:

if (score >= 90) {
    System.out.println("Your score is " + score + " which is an A");
} else if (score >= 80){
    System.out.println("Your score is " + score + " which is an B");
}//and so on

其他提示

It is simple, if the input is not bigger than 90, then nothing is printed. Use else ifs rather than this approach.

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top