java - how to automatically populate a 2d array with numbers -
hi trying auto populate 2d array based on user input. user enter 1 number, number set size of 2d array. want print out numbers of array. example , if user enters number 4 . 2d array 4 rows 4 colums, , should contain number 1 16, , print out follows.
1-2-3-4 5-6-7-8 9-10-11-12 13-14-15-16
but struggling think of right statement this. moment code prints out 2d array containing *.
has ideas how print out numbers , i'm stuck. code follows:
public static void main(string args[]){ scanner input = new scanner(system.in); system.out.println("enter room length"); int num1 = input.nextint(); int num2 = num1; int length = num1 * num2; system.out.println("room "+num1+"x"+num2+"="+length); int[][] grid = new int[num1][num2]; for(int row=0;row<grid.length;row++){ for(int col=0;col<grid[row].length;col++){ system.out.print("*"); } system.out.println(); } }
read n value,
int[][] arr = new int[n][n]; int inc=1; for(int i=0;i<n;i++) for(int j=0;j<n;j++) { arr[i][j]=inc; inc++; }
Comments
Post a Comment