Java - Write code to generate a two dimensional array and fills it with random numbers.

Write code to generate a two dimensional array and fills it with random numbers.

Description

Write code to generate a two dimensional array and fills it with random numbers.

Have your program multiply the random number against a random number generated at the beginning.

The output should look as follows:

16   51    4 52 60 68 48 20 8 92 
576 551  240 576 120 48 144 216 24 48 
36   11  90 72 138 54 30 54 114 96 
...

You can use the following code structure and fill in your logics.

public class Main {

  public static void main(String[] args) {
     //your code here
  }
}

Solution

Demo

public class Main {

  public static void main(String[] args) {
    int[][] matrix = new int[10][10];
    int randomValue = (int) (Math.random() * 30);

    for (int outerLoop = 0; outerLoop < matrix.length; outerLoop++) {
      for (int innerLoop =0; innerLoop < matrix[outerLoop].length ; innerLoop ++) {
        matrix[outerLoop][innerLoop] = (int) (Math.random() * 30);
        matrix[outerLoop][innerLoop] = matrix[outerLoop][innerLoop] * randomValue;
      }//  w  w  w  . ja  v a 2 s  .  c  om
    }
    
    for (int outerLoop = 0; outerLoop < matrix.length; outerLoop++) {
      for (int innerLoop =0; innerLoop < matrix[outerLoop].length ; innerLoop ++) {
        //System.out.print(matrix[outerLoop][innerLoop] + " ");
        System.out.format("%5d ",matrix[outerLoop][innerLoop] );
      }
      System.out.println();
    }
    

  }

}

Related Topic