问题描述
我需要帮助从if语句打印多行到控制台。我现在的代码在下面,但是它只会打印平均考试成绩和学生所得到的内容,而不会显示前三个考试成绩的结果。有人知道问题出在哪里吗?
import java.util.Scanner;
public class TestscoresAndGrade {
public static void main(String[] args) {
Scanner keyboard = new Scanner(system.in);
// Gathering info from user
System.out.println("Enter your first test score!");
int firstscore = keyboard.nextInt();
System.out.println("Enter your second test score!");
int secondscore = keyboard.nextInt();
System.out.println("Enter your third test score!");
int thirdscore = keyboard.nextInt();
int averageTestscore = (firstscore * secondscore * thirdscore)/3;
// Telling the student what grade they got on the first test
if (firstscore >= 90) {
System.out.println("Congrats you got an A on your first test!");
}
else if (firstscore < 90 && firstscore > 80) {
System.out.println("You got a B on your first test!");
}
else if (firstscore < 80 && firstscore > 70) {
System.out.println("You got a C on your first test!");
}
else if (firstscore < 70 && firstscore > 60) {
System.out.println("You got a D on your first test!");
}
else if (firstscore < 60) {
System.out.println("You got a F on your first test!");
}
// Telling student what grade they got on the second test
if (secondscore >= 90) {
System.out.println("Congrats you got an A on your first test!");
}
else if (secondscore < 90 && secondscore > 80) {
System.out.println("You got a B on your first test!");
}
else if (secondscore < 80 && secondscore > 70) {
System.out.println("You got a C on your first test!");
}
else if (secondscore < 70 && secondscore > 60) {
System.out.println("You got a D on your first test!");
}
else if (secondscore < 60) {
System.out.println("You got a F on your first test!");
}
// Telling student what they got on third test
if (thirdscore >= 90) {
System.out.println("Congrats you got an A on your third test!");
}
else if (thirdscore < 90 && thirdscore > 80) {
System.out.println("You got a B on your third test!");
}
else if (thirdscore < 80 && thirdscore > 70) {
System.out.println("You got a C on your third test!");
}
else if (thirdscore < 70 && thirdscore > 60) {
System.out.println("You got a D on your third test!");
}
else if (thirdscore < 60) {
System.out.println("You got a F on your third test!");
}
//Telling a student what there average score was
if (averageTestscore >= 90) {
System.out.println("Congrats your avergage is an A!");
}
else if (averageTestscore < 90 && averageTestscore > 80) {
System.out.println("Your average is a B!");
}
else if (averageTestscore < 80 && averageTestscore > 70) {
System.out.println("Your average is a C!");
}
else if (averageTestscore < 70 && averageTestscore > 60) {
System.out.println("Your average is a D!");
}
else if (averageTestscore < 60) {
System.out.println("Your average is an F!");
}
}
}
解决方法
正如@khelwood所指出的,您将从if-else块中排除80、70、60。如果您可以提供测试数据的样本,我们将更好地了解发生了什么。
在旁注
- 我们通常使用if-else if -... else。(按照惯例,最后一个分支是else,除非您要有意地排除某些情况)
- 就像@khelwood所说的那样,平均公式是错误的。还有一件事,如果要获取小数,则应为/3.0,因为您正在此处进行3的整数除。
- 如果要提高可读性,请考虑封装重复的逻辑并使之可重复使用。