您好,我正在尝试根据用户输入自动填充二维数组。 用户将输入一个数字,此数字将设置二维数组的大小。然后,我想打印出数组中的数字。 例如,如果用户输入数字4,则二维数组将为4行4列,并应包含数字1到16,并按以下方式打印。
1-2-3-4
5-6-7-8
9-10-11-12
13-14-15-16
但我很难想出正确的语句来完成此任务。目前我的代码只能打印一个包含*的二维数组。
有人有什么想法可以打印数字吗?我真的陷入了困境。 以下是我的代码:
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();
}
}
grid
吗? - Ankit