简体   繁体   中英

How do you find the maximum and minimum for a random array?

   public class ArrayCalculations
   {
      public static void main(String[]  args)
      {

         int sum = 0, smallest, largest;
         double average;
         int[] array = new int[20];

         for(int i = 0; i < array.length; i++)
         {

            array[i] = (int)(Math.random() * 500 + 1);
            System.out.println(array[i]);
            sum += array[i];
            smallest = array[0];
            largest = array[];
            if(array[i]<smallest)
            {
               smallest = array[i];
            }
            if(array[i]>largest)
            {
            largest = array[i];
            }

         }
         average = 1.0*sum/array.length;

         System.out.println("Sum: " + sum);
         System.out.println("Average: " + average);
         System.out.println("Smallest: " + smallest);
         System.out.println("Largest: " + largest);
      }
   }

It keeps saying that smallest and largest may not have been initialized. I am not sure how to fix it. I tried setting smallest and largest to the first number outside of the for loop but that was before the all the numbers in the array were initialized so that didn't work...

You're basically resetting the values of smallest and largest to the first element in the for loop (considering largest = array[] is a typing error).

Yes you need to initialize them outside the loop.

smallest = Integer.MAX_VALUE;
largest = Integer.MIN_VALUE;
for(int i = 0; i < array.length; i++) {
    array[i] = (int)(Math.random() * 500 + 1);
    System.out.println(array[i]);
    sum += array[i];
    if(array[i] < smallest) smallest = array[i];
    if(array[i] > largest)  largest = array[i];
}

There are two problems here:

  • Inside of a method (like main ), you cannot leave variables uninitialized that you intend to use later. The reason that Java isn't initializing those variables is due to the possibility of skipping the loop; even though it's unlikely in this scenario, Java believes that it's still a possibility.

  • The largest value you've ever seen in that list is negative infinity, and the smallest value that you've ever seen in that list is infinity (that is, you don't know exactly where it may lurk). Initialize those two variables to the smallest and largest value you can.

     int sum = 0, smallest = Integer.MAX_VALUE, largest = Integer.MIN_VALUE;
        largest = array[];
                  ^^^^^^^

Why are you making largest an array, then trying to treat it as a simple integer?

        if(array[i]>largest)
           ^^^^^^^^----integer
                    ^^^^^^^---array

A better structure would be

array[0] = (int)(Math.random() * 500 + 1);
      ^---note 0 index
largest = array[0];
smallest = array[0];

for(i = 1; ...) {
        ^--start at index #1
   array[i] = (int)(Math.random() * 500 + 1);
   if (array[i] < smallest) {
     smallest = array[i];
   }
   if (array[i] > largest) {
     largest = array[i];
  }
}

Basically, do the initialization OUTSIDE the loop.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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