質問

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