简体   繁体   English

如何在随机 class 生成的数组中找到最小值?

[英]How to find the lowest value in an array generated from the random class?

We have to pass the array through a method that takes the highest, lowest, and average then have them print.我们必须通过一种方法传递数组,该方法采用最高、最低和平均值,然后打印它们。

I'm having issues with getting the lowest value assigned to minNum.我在获取分配给 minNum 的最低值时遇到问题。

Thank you!谢谢!

public static void averageMaxMinMethod(double[] arrayOfDoubles){
   
      double maxNum = 0;
      double minNum = 0;
   
      for(int i = 0; i < arrayOfDoubles.length; i++){
      
         if(arrayOfDoubles[i] > maxNum)
            maxNum = arrayOfDoubles[i];
         if(arrayOfDoubles[i] < minNum)// <=== My poor attempt of getting the lowest value
            minNum = arrayOfDoubles[i];
      }
    System.out.println("Highest given double is: " + maxNum);
    System.out.println("Lowest given double is: " + minNum);
   }
   
   public static void main(String[] args) {   
   //1-3: Set up Scanner object to collect user's input on the size of array.
      Scanner keyboard = new Scanner(System.in);
      System.out.println("How many double numerical entries do you have?");
   //4: Declare an array of size sizeOfArray
      int sizeOfArray = keyboard.nextInt();  
   
      //Initialize array
      double[] arrayOfDoubles;
      arrayOfDoubles = new double[sizeOfArray];   
      
      
      
   
      for(int i = 0; i < sizeOfArray; i++){
      //5: Use the Random number Class and walk over the array 
         Random randomNum = new Random();       
         arrayOfDoubles[i] = randomNum.nextDouble(0.0 , 100.0); 
      }
      //6: Invoke SumMethod
      sumMethod(arrayOfDoubles);
      averageMaxMinMethod(arrayOfDoubles);
      
      
   }
  double maxNum = 0;
  double minNum = 0;

This only works if the maximum number is at least zero, and the minimum value is at most zero - otherwise, these values are not updated.这仅在最大数量至少为零且最小值至多为零时才有效 - 否则,这些值不会更新。

Instead, use the first element in the array, which is at least as large as the minimum, and at least as small as the maximum.相反,使用数组中的第一个元素,它至少与最小值一样大,并且至少与最大值一样小。

  double maxNum = arrayOfDoubles[0];
  double minNum = arrayOfDoubles[0];

Others have pointed out the mistake you made in your current implementation, however you also forgot to calculate the average.其他人指出了您在当前实现中犯的错误,但是您也忘记了计算平均值。 You don't actually have to write code to do that, Java includes DoubleSummaryStatistics which can give you the max , the min , and the average .您实际上不必编写代码来执行此操作,Java 包括DoubleSummaryStatistics可以为您提供maxminaverage Like,喜欢,

public static void averageMaxMinMethod(double[] arrayOfDoubles) {
    DoubleSummaryStatistics dss = DoubleStream.of(arrayOfDoubles).summaryStatistics();
    System.out.println("Highest given double is: " + dss.getMax());
    System.out.println("Lowest given double is: " + dss.getMin());
    System.out.println("Average is: " + dss.getAverage());
}

If you need to max, min and average values you can use DoubleStream and DoubleSummaryStatistics :如果您需要最大值、最小值和平均值,您可以使用DoubleStreamDoubleSummaryStatistics

double[] arrayOfDoubles = {5.1, 3.7, -2.9, 4.3, -11.2};

DoubleSummaryStatistics statistics = Arrays.stream(arrayOfDoubles).summaryStatistics();

System.out.println("Highest: " + statistics.getMin());
System.out.println("Lowest: " + statistics.getMax());
System.out.println("Average: " + statistics.getAverage());

Output: Output:

Highest: -11.2
Lowest: 5.1
Average: -0.2

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM