-4

编写一个程序,接受用户想要输入的整数。确定最高和最低数字,并从最高数字中减去所有其他数字。我无法理解差异。我知道这里不见了。请帮我。

import java.util.*;
//Misty Stewart
public class HighestLowestDifference2324 {

    public static void main(String[] args) {
        Scanner input = new Scanner(System.in);

        double maxValue = Double.MIN_VALUE;
        double minValue = Double.MAX_VALUE;
        double minValue1 = Double.MAX_VALUE;
        double minValue2 = Double.MAX_VALUE;

        int count = 0;
        double difference = 0;

        System.out.println("Enter numbers. Terminate with Q.");
        while (input.hasNextDouble()) {

            double Value = input.nextDouble();

            if (Value > maxValue) {
                maxValue = Value;
            }
            if (Value < minValue) {
                minValue = Value;
            }
            if (Value < minValue) {
                minValue1 = Value;
            }
            if (minValue == minValue1) {
                minValue = Value;
            }
            if (Value < minValue1) {
                minValue2 = Value;
            }
            if (minValue1 == minValue2) {
                minValue1 = Value;
            }
        }
        count++;
        difference = maxValue - minValue;
        {
            if (count > 0) {
                System.out.println("Highest no. is: " + maxValue);
                System.out.println("Lowest no. is: " + minValue);
                System.out.println("The difference from the highest is: \n" + difference);
            } else {
                System.out.println("Have a good day!");
            }
        }
    }
}
4

4 回答 4

1

您需要将用户输入的值存储在一个数组中。然后,一旦你有了maxVAlue,用循环遍历数组并计算并显示差异。

于 2013-03-07T11:41:49.583 回答
0
while (input.hasNextDouble()) {
 num = input.nextDouble();
 if ( num > max ) max = num;
 if ( num < min ) min = num;
}

difference = max -min;

上面的代码可用于从数字列表中获取最高、最低和它们的差异

于 2013-03-07T11:42:24.263 回答
0

您可能应该将所有输入数字存储在 aCollection中,例如 an ArrayList,然后进行分析:

ArrayList<Double> list = new ArrayList<Double>();
while (input.hasNextDouble()) {
  double value = input.nextDouble();

  // logic for determining largest & smallest

  list.add(value);
}

然后迭代ArrayList以获得您想要的输出。

于 2013-03-07T11:43:31.530 回答
0

这个怎么样:

    double maxValue = Double.MIN_VALUE;
    double minValue = Double.MAX_VALUE;
    List<Double> allValues = new ArrayList<Double>();
    while (input.hasNextDouble()) {
        double value = input.nextDouble();
        maxValue = Math.max(maxValue, value);
        minValue = Math.min(minValue, value);

        allValues.add(value);
    }

    System.out.println("Highest no. is: " + maxValue);
    System.out.println("Lowest no. is: " + minValue);
    System.out.println("The difference from the highest is:" + (maxValue - minValue));
    for (Double value : allValues) {
        System.out.println("The difference from the highest is:" + (maxValue - value));
    }
于 2013-03-07T11:56:25.303 回答