我是Java新手,正在从事学校作业。当我运行该程序时,它将给出测试成绩的平均值,但是当我输入“ 999”退出时。它给了我一个平均值='NaN',但我需要说0
import java.util.Scanner;
public class TestScoreApp {
public static void main(String[] args) {
// display operational messages
System.out.println("Enter test scores that range from 0 to 100.");
System.out.println("To end the program, enter 999.");
System.out.println(); // print a blank line
// initialize variables and create a Scanner object
int scoreTotal = 0;
int scoreCount = 0;
int testScore = 0;
Scanner sc = new Scanner(System.in);
// get a series of test scores from the user
while (testScore != 999) {
// get the input from the user
System.out.print("Enter score: ");
testScore = sc.nextInt();
// makes average score 0
if (999 == testScore) {
double averageScore;
averageScore = 0;
}
// accumulate score count and score total
if (testScore <= 100) {
scoreCount = scoreCount + 1;
scoreTotal = scoreTotal + testScore;
} else
System.out.println("Invalid entry: not counted");
}
// display the score count, score total, and average score
double averageScore = (double) scoreTotal / scoreCount;
if (averageScore == 0) {
System.out.println("0");
}
String message = "\n"
+ "Score count: " + scoreCount + "\n"
+ "Score total: " + scoreTotal + "\n"
+ "Average score: " + averageScore + "\n";
System.out.println(message);
}
}
我已经在这个程序上工作了3个小时,我们将不胜感激。甚至重定向到已归档的帖子也将很有帮助。
答案 0 :(得分:1)
您在999
之前输入了其他分数吗?如果不是,您的scoreCount
将为零,并且在计算平均值时将被零除。解决方案:在scoreTotal
时不要将scoreCount
除以scoreCount == 0
。在这种情况下,平均值仅为零:
double averageScore = 0;
if (scoreCount != 0)
avergageScore = scoreTotal / scoreCount;
// or, if you like the ternary operator better:
double averageScore =
(scoreCount == 0)
? 0
: (scoreTotal / scoreCount);
答案 1 :(得分:0)
在进入double avarageScore
循环之前,只需创建while
。
import java.util.Scanner;
public class TestScoreApp {
public static void main(String[] args) {
// display operational messages
System.out.println("Enter test scores that range from 0 to 100.");
System.out.println("To end the program, enter 999.");
System.out.println(); // print a blank line
// initialize variables and create a Scanner object
int scoreTotal = 0;
int scoreCount = 0;
int testScore = 0;
Scanner sc = new Scanner(System.in);
double averageScore;
并添加if(scoreCount==0){}
条件以检查是否有以前的输入,因此它不会被零除零。
// get a series of test scores from the user
while (testScore != 999) {
// get the input from the user
System.out.print("Enter score: ");
testScore = sc.nextInt();
// makes average score 0
if (999 == testScore) {
//double averageScore;
averageScore = 0;
}
// accumulate score count and score total
if (testScore <= 100) {
scoreCount = scoreCount + 1;
scoreTotal = scoreTotal + testScore;
} else
System.out.println("Invalid entry: not counted");
}
// display the score count, score total, and average score
if(scoreCount==0) {
averageScore = 0;
}else {
averageScore = (double) scoreTotal / scoreCount;
}
}
String message = "\n"
+ "Score count: " + scoreCount + "\n"
+ "Score total: " + scoreTotal + "\n"
+ "Average score: " + averageScore + "\n";
System.out.println(message);
}
}
我在计算机上进行编译,结果为999
Enter test scores that range from 0 to 100.
To end the program, enter 999.
Enter score: 50
Enter score: 50
Enter score: 50
Enter score: 42
Enter score: 999
Invalid entry: not counted
Score count: 4
Score total: 192
Average score: 48.0
如果我第一次输入999,输出为:
Enter test scores that range from 0 to 100.
To end the program, enter 999.
Enter score: 999
Invalid entry: not counted
Score count: 0
Score total: 0
Average score: 0.0
正如诚诚指出的那样:“ NaN表示“不是数字”,并且是对浮点数进行未定义操作的结果,例如将零除以零。(请注意,将非零数字除以零也是通常在数学中未定义,它不会导致NaN,但会导致正无穷大或负无穷大”。
如果您将999输入为第一个输入,则会得到平均得分:NaN,因为它除以0/0。