我试图从一系列双打中获得最小值和最大值。最大值工作正常,但最小值似乎总是为零。获得最低价值的最佳方法是什么?请注意,我不能在本练习中使用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); }}
慕村225694
开满天机
相关分类