我有一个Java分配来制作如下评分等级。
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
}
}
我可以让它在Eclipse中工作但它会在我输入数字后终止。我究竟做错了什么?
答案 0 :(得分:4)
您必须使用if
和else if
,因为如果第一个条件不匹配,它将退出并且不会达到嵌套(其他)条件:
尝试将其更改为:
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
答案 1 :(得分:1)
很简单,如果输入不大于90,则不打印任何内容。
使用else if
而不是这种方法。