【问题标题】:Finding the lowest value in the array查找数组中的最小值
【发布时间】:2018-09-24 20:02:38
【问题描述】:
int min = temperature[0];
//Here, i have initialized the minium array. 
else if(temperature[i] < min) {
            min = temperature[i];

在这里我比较了数组中的值。但是由于 min 的初始化为 0。它一直将最小值为零。我如何解决它。这是我的整个代码。

 int[] temperature = new int[12];
    Scanner kb = new Scanner(System.in);

    int min = temperature[0];
    int max = temperature[0];

    int counter = 0;
    for (int i = 0; i < temperature.length; i++) {
        System.out.println("Please enter the temperature:" + i);
        temperature[i] = kb.nextInt();
        counter += temperature[i];
        if (temperature[i] > max) {
            max = temperature[i];
        }
        else if(temperature[i] < min) {
            min = temperature[i];

        }
    }
    int average = counter / temperature.length;
    System.out.println("Displaying the average temperature:" + average);
    System.out.println("The lowest temperature is:" + min);
    System.out.println("The highest temperaature is:" + max);
    }
}

【问题讨论】:

  • "但是由于 min 的初始化是 0。它会一直到最小值为零。" - 那么不要用 0 初始化它。
  • 你的数组中实际有哪些数据?
  • @Makoto数组中的数据只是用户输入的数字,例如温度。
  • 因为 min 的初始化为 0:不是。它用温度[0]初始化。只是您在用户输入值之前初始化最小值,而不是之后
  • 作为最小值的初始猜测,您应该选择一个较高的值,例如Integer.MAX_VALUE。否则你的数组元素没有机会低于最初的猜测。注意有Collections.min(Arrays.asList(myArray)) 和类似的。

标签: java arrays max average minimum


【解决方案1】:

如果数组至少有一个值,则解决方案是将 min 初始化为数组的第一个值。如果你真的想要,你也可以将它设置为 Integer.MAX_VALUE。

【讨论】:

    【解决方案2】:

    删除else,逻辑上 - 如果该值小于当前最小值,我们要更新当前最小值(无论当前最大值的状态如何)。我们实际上可以使用Math.max(int, int)Math.min(int, int) 使其更清晰。而且,我们不能在没有读取输入的情况下默认 minmax 为初始值(除非我们使用明确荒谬的值)。喜欢,

    int[] temperature = new int[12];
    Scanner kb = new Scanner(System.in);
    
    int min = Integer.MAX_VALUE, max = Integer.MIN_VALUE, counter = 0;
    for (int i = 0; i < temperature.length; i++) {
        System.out.println("Please enter the temperature:" + i);
        temperature[i] = kb.nextInt();
        counter += temperature[i];
        max = Math.max(max, temperature[i]);
        min = Math.min(min, temperature[i]);
    }
    int average = (int) (counter / (double) temperature.length);
    System.out.println("Displaying the average temperature:" + average);
    System.out.println("The lowest temperature is:" + min);
    System.out.println("The highest temperaature is:" + max);
    

    否则,您需要两个循环。最后,请注意您在计算 average 时使用了整数数学。您可能想要浮点数学。

    或者,更好的是,使用IntSummaryStatisticslike

    int[] temperature = new int[12];
    Scanner kb = new Scanner(System.in);
    for (int i = 0; i < temperature.length; i++) {
        System.out.println("Please enter the temperature:" + i);
        temperature[i] = kb.nextInt();
    }
    IntSummaryStatistics iss = IntStream.of(temperature).summaryStatistics();
    
    System.out.println("Displaying the average temperature:" + iss.getAverage());
    System.out.println("The lowest temperature is:" + iss.getMin());
    System.out.println("The highest temperaature is:" + iss.getMax());
    System.out.println("The total of all values is:" + iss.getSum());
    

    【讨论】:

    • 如果你用第一个数组元素初始化minmaxif-else if实际上是正确的,因为在开始min == max。由于min 只会变小,而max 会变大,因此某些值可以更新min 或更新max,但不能同时更新。
    • @Turing85 这就是我所做的“两个循环”评论。
    • 你可以用一个循环和if-else if :D 但Math.min(...).Math.max(...) 变体无论如何都更具可读性
    • @Turing85 重新考虑后,我会使用IntSummaryStatistics
    • 我想使用非常基础的 Java,因为我正在学习这门语言。但在得到一些提示后,我对源代码进行了修改。如上所述,我使用了两个循环。但问题并没有解决。
    猜你喜欢
    • 1970-01-01
    • 2021-09-15
    • 2015-04-09
    • 1970-01-01
    • 2018-05-03
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多