我正在编写一个方法,它接受用户整数输入并显示总计,平均值,最大值和最小值。无法获取用户输入的正确最大值和最小值
我有总计和平均工作,但我得到的最大值为2147483647,最小值为-2147483648。
循环只能在用户输入-1时结束。
我的代码:
public static void processNumbers()
{
Menu m = new Menu();
clrscr();
int count = 0; // Number of times a value has been added
int num = 0; // The Integer that the user inputs
int tot = 0; // The total sum of the inputs
int avg = 0; // The average value of the inputs
int max = Integer.MAX_VALUE; // The maximum value of the inputs
int min = Integer.MIN_VALUE; // The minimum value of the inputs
System.out.println ("Please enter a whole number (e.g. 150)");
while ((num = Genio.getInteger()) != -1)
{
count ++;
tot += num;
avg = tot/count; //Calculate the average the while loop
if(tot>max) max = tot;
if(tot<min) min = tot;
System.out.println("Total \t Average\t Maximum\t Minimum\n");
System.out.println(tot + "\t" + avg + "\t\t" + max + "\t" + min + "\n");
}
clrscr();
System.out.println("You entered -1, you will now return to the menu!");
pressKey();
m.processUserChoices();
}
谢谢!学到了新东西,非常感谢! – DarkBlueMullet 2014-11-04 19:48:42