Java 爪哇分级表
我有一个Java作业,用来制作一个如下所示的等级Java 爪哇分级表,java,variable-assignment,Java,Variable Assignment,我有一个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
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中工作,但在我输入一个数字后它将终止。我到底做错了什么?您必须使用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
您必须使用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
很简单,如果输入不大于90,则不打印任何内容。
使用else ifs而不是这种方法。很简单,如果输入不大于90,则不打印任何内容。
使用else ifs而不是这种方法。什么都不打印??如果不是嵌套ifs,我认为您希望使用else。打印B、C、D、F是不可能的,因为只有当你的分数超过90分时,它才会出现在第一个if语句中。另外,我建议不要在你的类名中使用另一个注释,它应该是GradeSelection而不是GradeSelection。@BigRabbit不是真的,因为几乎每个右括号都会关闭if,而B注释应该提供上下文,不反映语法。@BigRabbit非构造性?我懂了;关于教育人们,思考代码,我知道些什么,对吗?祝你好运没有打印任何内容??如果没有嵌套的ifs,我认为您希望使用其他内容。打印B、C、D、F是不可能的,因为只有当你的分数超过90分时,它才会出现在第一个if语句中。另外,我建议不要在你的类名中使用另一个注释,它应该是GradeSelection而不是GradeSelection。@BigRabbit不是真的,因为几乎每个右括号都会关闭if,而B注释应该提供上下文,不反映语法。@BigRabbit非构造性?我懂了;关于教育人们,思考代码,我知道些什么,对吗?祝你好运此外,您不需要检查它是否小于90,因为您在上一个if中检查了它。此外,您不需要检查它是否小于90,因为您在上一个if中检查了它。