我想确定每个用户提供的输入是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
。
如果没有给出输入,第二种解决方案也将摆脱第一种解决方案的奇怪行为(在这种情况下,第一种解决方案将具有“低”>“高”)。 +1 –
是@JiriTousek。但是,这只是一个角落案例,可以照顾程序员也想要的任何方式。它从编码器到编码器。 – 97amarnathk