【发布时间】:2018-07-08 16:59:27
【问题描述】:
我正在尝试编写一个程序,允许用户输入他们想要显示的多个随机等级以及生成列表的总和、平均值、最小值和最大值。分数范围从 60 - 100。
程序正在正确打印最小值,并且总和将先前生成的总和与新生成的总和相加。我怎样才能改变它,以便它给出正确的最小输出,从而停止将以前的总和添加到新的总和中?任何帮助将不胜感激。
输出的图片链接显示了最小的输出问题。最小值应该是 66.0,但它说的是 59.0。 output 导入 java.util.Scanner;
public class A03C
{
public static void main(String[] args)
{
Scanner input = new Scanner(System.in);
System.out.print("How many scores? ");
int howMany = input.nextInt();
double score = 0;
double sum = 0;
double average = 0;
double max = 0;
double min = 60;
while (howMany > 0)
{
for (int i = 1; i <= howMany; i++)
{
score = 60 + (int)(Math.random() * ((100 - 60) +1));
if (letterGrade(score));
sum += score++;
average = (sum/howMany);
if (score > max)
max = score;
if (score < max)
min = score;
}
System.out.println("Sum: " + sum);
System.out.println("Average: " + average);
System.out.println("Max: " + (max - 1));
System.out.println("Min: " + (min - 1));
System.out.println("How many scores? ");
howMany = input.nextInt();
}
}
public static boolean letterGrade(double score)
{
if (score >= 92.0)
System.out.println(score + " is an A");
else if (score >= 83.0)
System.out.println(score + " is a B");
else if (score >= 75.0)
System.out.println(score + " is a C");
else
System.out.println(score + " is an F");
return false;
}
}
【问题讨论】:
-
看来你需要学习使用调试器了。请帮助自己一些complementary debugging techniques。如果您之后仍有问题,请随时回来提出更具体的问题。
-
“遇到麻烦”是一个含糊不清的问题描述。请edit您的问题包括您遇到的问题。你得到编译错误吗?运行时错误?输出与预期不同?等等。请不要忘记包含您遇到的任何错误的全文,以及描述不当行为时的预期与实际输出。