문제

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