输出一个我不想得到的条件,可能是代码块管理不当
Outputting a condition I didn't mean to get, probably code blocks are not managed properly
Java 的新手,在使用积木时遇到困难,当输入 sum = 9、examGrade = 105 和 average = 105 时,输出应该仅为“错误无效数字”。
现在的输出是:
“错误号码无效”
100
100.00
不知何故进入了else条件,没能成功,先谢谢大家的帮助:)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int examGrade = 0, sum;
double average = 0;
System.out.println("Please enter your Exam Grade");
examGrade = input.nextInt();
System.out.println("Please enter your homework average");
average = input.nextDouble();
System.out.println("Please enter number of exercise");
sum = input.nextInt();
if (sum > 8 || examGrade > 100 || average > 100) {
System.out.println("Error invaild numbers");
}
if (sum <= 4) {
System.out.println("Your Final grade is zero");
}
if (sum == 5 || sum == 6) {
if (examGrade >= 55) {
System.out.println(examGrade*0.8 + average*0.2);
}
}
else {
System.out.println(examGrade);
}
if (sum == 7 || sum == 8) {
if(examGrade <= 54) {
if(average >= 80) {
System.out.println(examGrade*0.75 + average*0.25);
}
else if(average < 80) {
System.out.println(examGrade*0.8 + average*0.2);
}
}
}
else {
System.out.println(examGrade*0.7 + average*0.3);
}
}
如果您遇到“无效数字”情况,您可能不想进行所有其他检查。为此,您需要像这样将它们全部包装到 else 中:
if (sum > 8 || examGrade > 100 || average > 100 ) {
System.out.println("Error invaild numbers");
} else {
if (sum <= 4 ) {
System.out.println("Your Final grade is zero");
}
// and so on with all the other ifs
} // finish the "not invalid" block
Java 的新手,在使用积木时遇到困难,当输入 sum = 9、examGrade = 105 和 average = 105 时,输出应该仅为“错误无效数字”。
现在的输出是:
“错误号码无效”
100
100.00
不知何故进入了else条件,没能成功,先谢谢大家的帮助:)
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
int examGrade = 0, sum;
double average = 0;
System.out.println("Please enter your Exam Grade");
examGrade = input.nextInt();
System.out.println("Please enter your homework average");
average = input.nextDouble();
System.out.println("Please enter number of exercise");
sum = input.nextInt();
if (sum > 8 || examGrade > 100 || average > 100) {
System.out.println("Error invaild numbers");
}
if (sum <= 4) {
System.out.println("Your Final grade is zero");
}
if (sum == 5 || sum == 6) {
if (examGrade >= 55) {
System.out.println(examGrade*0.8 + average*0.2);
}
}
else {
System.out.println(examGrade);
}
if (sum == 7 || sum == 8) {
if(examGrade <= 54) {
if(average >= 80) {
System.out.println(examGrade*0.75 + average*0.25);
}
else if(average < 80) {
System.out.println(examGrade*0.8 + average*0.2);
}
}
}
else {
System.out.println(examGrade*0.7 + average*0.3);
}
}
如果您遇到“无效数字”情况,您可能不想进行所有其他检查。为此,您需要像这样将它们全部包装到 else 中:
if (sum > 8 || examGrade > 100 || average > 100 ) {
System.out.println("Error invaild numbers");
} else {
if (sum <= 4 ) {
System.out.println("Your Final grade is zero");
}
// and so on with all the other ifs
} // finish the "not invalid" block