【发布时间】:2018-03-03 16:32:01
【问题描述】:
我试图确定用户提供的每个输入是否是他们所有输入中的max 或min,然后将该输入分配给变量high 或low
int inputnum = 0;
double sum = 0;
double lastinput = 0;
double high;
double low;
double average;
Scanner input = new Scanner(System.in);
high = 0;
low = 0;
do {
System.out.println("Enter a number. Type 0 to quit.");
lastinput = input.nextDouble(); //reads input
sum += lastinput; //add to sum
if (lastinput != 0) {
inputnum += 1; //counts number of inputs (except 0)
}
if (lastinput > high && lastinput != 0) {
high = lastinput;
}
if (lastinput < low && lastinput != 0) {
low = lastinput;
}
average = (sum / inputnum);
} while (lastinput !=0); //repeat unless user inputs 0
问题是我不能在不给它赋值的情况下声明变量(例如 0)。例如,如果用户输入3、5 和7,则low 的值仍定义为0。
【问题讨论】: