简体   繁体   中英

How do I get the code to print random numbers in all the elements of the 2d array?

I got the 2d array to print but with all zero's and the only random number comes up on the bottom right corner

How do I get the code to print random numbers in all the elements of the 2d array?

Here is my code:

public static void main(String[] args) {
    int columns = 8;
    int rows = 4;
    int rLow = 2;
    int rHigh = 9;
    printRandos(columns, rows, rLow, rHigh);
}

public static void printRandos(int clmn, int rws, int rlow, int rhigh) {
    Random rando = new Random();
    int randoNum = rlow + rando.nextInt(rhigh);
    int[][] randoArray = new int[rws][clmn];
    for (int i = 0; i < rws; i++) {
        for (int k = 0; k < clmn; k++) {
            randoArray[rws - 1][clmn - 1] = randoNum;
            System.out.print(randoArray[i][k] + " ");
        }
        System.out.print("\n");
    }
}

Your bug is in this line:

          randoArray[rws-1][clmn-1] = randoNum;

This stores your random number into randoArray[rws-1][clmn-1] each time, which as you noticed, is the bottom right corner. rws is always 4, and clmn is always 8. So you store the same number there 32 times, which gives the same result as storing it only once.

In the following line you are correctly printing the number from the current array location:

          System.out.print(randoArray[i][k]+" ");

An int array comes initialized with all zeroes, and since except for the last corner you have not filled anything into your array, 0 is printed.

Also if you want different random numbers in all the cells, you would need to call rando.nextInt() inside your innermost for loop.

Unless you need this 2-D array for some purpose (which doesn't show from the minimal example code that you have posted), you do not need it for printing a matrix of random numbers, ie, you may just print the numbers form within your loop without putting them into the array first.

Finally if rhigh should be the highest possible random number in the array, you should use rando.nextInt(rhigh - rlow + 1) . With rlow equal to 2 and rhigh equal to 9 this will give numbers in the range from 0 inclusive to 9 - 2 + 1 = 8 exclusive, which means that after adding to rlow = 2 you will get a number in the range from 2 to 10 exclusive, in other words, to 9 inclusive.

I am on purpose leaving to yourself to fix your code based on my comments. I believe your learning will benefit more from working it out yourself.

for (int i = 0; i < rws; i++)
{
    for (int k = 0; k < clmn; k++)
    {

       int randoNum = rlow + rando.nextInt(rhigh);
       randoArray[i][k] = randoNum;
               
       System.out.print(randoArray[i][k]+" ");
    }
    System.out.print("\n");
}

your mistake inside the inner for loop of the printRandos method. Firstly your random number is outside the loop so your array elements were receiving the same number all the time. Another mistake is that you are assigning the value to the same array element all the time ie rws-1 and clmn-1. inside your inner loop replace it with this:

           int randoNum = rlow + rando.nextInt(rhigh);
           randoArray[i][k] = randoNum;
           System.out.print(randoArray[i][k]+" ");

Your assign the array value outside the array length

 int[][] randoArray = new int[rws][clmn];
 randoArray[rws][clmn] = randoNum;

Here randoArray[rws] is out of bounds.

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