简体   繁体   中英

Random int array not printing min and max

I am new to Java (and coding in general). I need to write a method that gets an integer, creates an array made of random numbers of the length of the integer, range 1-50 (inclusive) and finds the maximum and minimum number.

I was able to fill the array with random numbers. However, my min and max are not printing. I have been looking at this for hours with no luck - any advice would be very helpful! Thank you.

 import java.util.Scanner; import java.util.Random; class MinMax { public static void main(String args[]) { Scanner in = new Scanner(System.in); //user input a number System.out.println("Please enter an integer from 1 to 50: "); int userNum = in .nextInt(); System.out.println(); int x; int randNum; int y; //create random numbers and print Random r = new Random(); for (int i = 1; i <= userNum; i++) { randNum = r.nextInt(50) + 1; System.out.println(randNum); int[] array = new int[userNum]; for (x = 1; x < 1; x++) { array[x] = randNum; System.out.println(array[x]); //find max int largest = array[0]; int smallest = array[0]; for (int s = 0; s < array.length; s++) { largest = array[s]; smallest = array[s]; if (array[s] > largest) { System.out.println(largest); //find min } else if (array[s] < smallest) { System.out.println(smallest); } //for } //if } //for } //for } //main method } //class 

This is what you looking for::

import java.util.Scanner;
 import java.util.Random;

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

    Scanner in = new Scanner(System.in);
    // user input a number
    System.out.println("Please enter an integer from 1 to 50: ");
    int userNum = in.nextInt();
    System.out.println();

    int x;
    int randNum;
    int y;
    int[] array = new int[userNum];
    // create random numbers and print
    Random r = new Random();
    for (int i = 1; i <= userNum; i++) {
        randNum = r.nextInt(50) + 1;
        System.out.println("number " + i + ":: " + randNum);

        array[i - 1] = randNum;

    }

    // print array
    for (int s = 0; s < array.length; s++) {
        System.out.println("array[" + s + "] number :: " + array[s]);
    }
    // find max
    int largest = array[0];
    int smallest = array[0];
    for (int s = 0; s < array.length; s++) {

        if (array[s] > largest) {
            largest = array[s];

            // find min
        } else if (array[s] < smallest) {
            smallest = array[s];

        }
    }
    System.out.println("\nLargets Number:: " + largest);
    System.out.println("Smallest Number:: " + smallest);

} // main method
} // class

Don't confuse too much when you are using loops.Some mistakes you done while writing code.

1> You have declared array int[] array = new int[userNum]; in for loop. which mean it will create as much arrays as loop iterate. and not accessible outside loop.

2> Same with variables largest and smallest int largest = array[0]; int smallest = array[0]; int largest = array[0]; int smallest = array[0];

3> for (x = 1; x < 1; x++) control never ever enter the loop since your condition x<1 is never satisfy because you have initiated x=1 and and it will never ever be less than 1 as in condition (x<1).

Suggestion :: To find largest you have to traverse through array and check every element for largest and smallest and store it in separate variable rather than printing it at every check.

In my code i have used three for loops::

1>> to generate and print random number. In same loop i'm filling array with generated number.

2>> Here just printing array separately for better presentation in output.

3>> Finding largest and smallest by assigning values in if block if they satisfy condition.

I hope this will help you to understand code.

    //find max
    int largest = array[0];
    int smallest = array[0];
    for (int s = 0; s < array.length; s++) {
      largest = array[s];
      smallest = array[s];
      if (array[s] > largest) {
        System.out.println(largest);
        //find min
      } else if (array[s] < smallest) {
        System.out.println(smallest);
      } //for
    } //if

Look closely at this code. Inside the loop, you are setting

largest = array[s];

and then immediately checking

if (array[s] > largest)

Of course it's not, you just made them equal!

Put the assignments inside the conditions:

      // Remove largest = array[s]
      if (array[s] > largest) {
        largest = array[s];
      }

Similarly for smallest.


Note: I would recommend against commenting on closing braces. Note only is it unnecessary effort, it is confusion-prone: in this snippet of code above, the //for and //if are reversed. Additionally, it doesn't tell you which "if" or "for" it is closing.

Simply looking at the indentation is an easier way.

Your program does not fill the array with random numbers. It creates several arrays and fills each of them with a single number. You should change this part of the program:

Random r = new Random();
for (int i = 1; i <= userNum; i++) {
    randNum = r.nextInt(50) + 1;
    System.out.println(randNum);
    int[] array = new int[userNum];
    for (x = 1; x < 1; x++) {
        array[x] = randNum;
        System.out.println(array[x]);

        /* min-max code */
    }
}

to this:

Random r = new Random();
int[] array = new int[userNum];
for (int i = 0; i <= userNum; i++) {
    array[i] = r.nextInt(50) + 1;
    System.out.println(array[i]);
}

/* min-max code */

This will create a single array, fill it with random numbers, and print each element of the array, which seems to be what you intended.

The code that finds the minimum and maximum is also incorrect.

int largest = array[0];
int smallest = array[0];
for (int s = 0; s < array.length; s++) {
    largest = array[s];
    smallest = array[s];
    if (array[s] > largest) {
        System.out.println(largest);
    } else if (array[s] < smallest) {
        System.out.println(smallest);
    }
}

You should only set largest or smallest when array[s] is actually the largest or smallest value in the array so far. To check whether or not this is the case, you can use Math.min() and Math.max() , which return the smallest or largest, respectively, of two values.

int largest = array[0];
int smallest = array[0];
for (int s = 0; s < array.length; s++) {
    largest = Math.max(array[s], largest);
    smallest = Math.min(array[s], smallest);
}
System.out.println(largest);
System.out.println(smallest);

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