【发布时间】:2014-12-31 20:14:20
【问题描述】:
我正在编写一个接受用户整数输入并显示总计、平均值、最大值和最小值的方法。
我有总工作量和平均工作量,但我得到的最大值为 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();
}
【问题讨论】:
标签: java loops while-loop