我试图从一系列双打中获得最小值和最大值。最大值工作正常,但最小值似乎总是为零。获得最低价值的最佳方法是什么?请注意,我不能在本练习中使用for循环,而是使用while循环。public class LoopingFloats {
public static void main(String[] args) {
double[] inputHolder = new double[5];
int inputCounter = 0;
double total = 0.0d;
double average = 0.0d;
double maximum = 0.0d;
double minimum = inputHolder[inputCounter];
double interestRate = 0.20d;
double interestAmount = 0.0d;
Scanner scnr = new Scanner(System.in);
while(inputCounter <= 4){
System.out.println("Enter number " + (inputCounter + 1) + ": ");
inputHolder[inputCounter] = scnr.nextDouble();
if(inputHolder[inputCounter] > maximum){
maximum = inputHolder[inputCounter];
}
if(inputHolder[inputCounter] < minimum){
minimum = inputHolder[inputCounter];
}
total = total + inputHolder[inputCounter];
inputCounter += 1;
}
}
average = total / 5;
interestAmount = total * interestRate;
System.out.println("Total: " + total);
System.out.println("Average: " + average);
System.out.println("Maximum: " + maximum);
System.out.println("Minimum: " + minimum);
System.out.println("Interest for total at 20%: " + interestAmount);
}}
3 回答
开满天机
TA贡献1786条经验 获得超13个赞
除了变量的初始化之外,一切似乎都没问题minimum
。在inputHolder[inputCounter] = scnr.nextDouble();
语句后的while循环中初始化它
inputHolder[inputCounter] = scnr.nextDouble(); if (inputCounter == 0) minimum = inputHolder[inputCounter];
希望这能解决你的问题。
添加回答
举报
0/150
提交
取消