ryan ryan - 6 months ago 15
Java Question

generating random numbers based on the comments array

I am having issues when writing this code, and I think i botched the whole thing.

Can someone point me in the right direction? Here is what I have so far

public class FArray {
// build an array of 10 numbers of random numbers, and then display them out
public static void main(String[] args) {
// declare an array (named numbs) that holds 10 ints
int[] numbs = new int[10];
// write a for loop that loops 10 times and generates a random number between 0 and 100
for (int i = 0; i < numbs.length; i++) {
numbs[i] = (int) (Math.random() * 100 + 1);

// assign the random number to each element in the array , numbs
numbs = new numbers();
// write a for loop that displays to the screen each random number in the array named numbs
numbers[i] = Numbers;
}
}
}

Answer

If you only want to print 10 number randomely you can do this using a stream of integer, in this stream we iterate from 0 to 10 and for each of the integer we print a random double between 0 and 100 :

IntStream.range(0, 10).forEach(i -> System.out.println(Math.random() * 100 + 1));

Edit : if you only want to insert 10 random values into your array numbs and after that printing them you only have to remove this two lines :

        numbs = new numbers();
        // write a for loop that displays to the screen each random number in the array named numbs
        numbers[i] = Numbers;

So your main method will look like this :

  public static void main(String[] args) {
    // declare an array (named numbs) that holds 10 ints
    int[] numbs = new int[10];
    // write a for loop that loops 10 times and generates a random number between 0 and 100
    for (int i = 0; i < numbs.length; i++) {
       numbs[i] = (int) (Math.random() * 100 + 1);
    }
    // print the values
    for (int number : numbs) {
       System.out.println(number);
    }
 }
Comments