【发布时间】:2016-07-31 12:18:40
【问题描述】:
我写了一个程序,提示用户输入10个成绩,它计算成绩的总和、成绩的平均值、最高成绩、最低成绩以及最高和最低之间的范围。然后它使用 system.out.print 打印所有这些东西。
我的问题是,除了最高价值之外,一切都有效。这是我的代码。
int x = 10; 诠释 i = 1; 整数总和 = 0; 整数猜测 = 100; 整数最大值 = 100;
while (i <= 10) {
x = Integer.parseInt(JOptionPane.showInputDialog("Enter one grade at a time."));
i++;
sum+=x;
if(x<guess){
guess = x;
}
if(x>max){
max = x;
}
}
System.out.println("The sum of your grades is " + sum);
System.out.println("The average of your grades is " + (sum/10));
System.out.println("The smallest grade is " +guess);
System.out.println("The highest grade is " +max);
System.out.println("The range of your grades is " + (max-guess));
假设输入的 10 个数字是 10,20,30,40,50,60,70,80,90,99,它会打印出“最高分是 100”,但没有输入 100。它总是显示 100。我该如何解决这个问题?谢谢!!
【问题讨论】:
-
考虑使用
IntSummaryStatistics。
标签: java loops if-statement statistics