gpt4 book ai didi

java - 试图制作一个双数组,编号为 2 到 50

转载 作者:行者123 更新时间:2023-11-30 09:30:10 25 4
gpt4 key购买 nike

我正在尝试使用 double 组制作一个 5 x 10 的表格。第一个框应该是空白的,然后其余的编号为 2-50。

目前我有这个,但它没有用。

int array[][] = new int[5][10];
for(int row=1; row<5;row++){
for(int col=1;col<10;col++)
array[row][col] = row*col;}
System.out.println(array);

最佳答案

row * col 无法为您提供从 2 到 50 的连续数字。在您的代码中,您不仅要离开 first box,而且要完全离开 first rowfirst column

您应该从 0 到 max 正常运行循环。对于 [0][0],不要打印任何内容。

此外,对于从 2 到 50 的打印,只需要一个以 2 开头的计数变量,并在打印后将其递增 1.

修改后的代码:-

int array[][] = new int[5][10];
int count = 2;

for(int row=0; row<5;row++){
for(int col=0;col<10;col++) {
if (row == 0 && col == 0) {
continue;
}
array[row][col] = count++;
}
}
for (int[] inner: array) {
System.out.println(Arrays.toString(inner));
}

输出:-

[0, 2, 3, 4, 5, 6, 7, 8, 9, 10]
[11, 12, 13, 14, 15, 16, 17, 18, 19, 20]
[21, 22, 23, 24, 25, 26, 27, 28, 29, 30]
[31, 32, 33, 34, 35, 36, 37, 38, 39, 40]
[41, 42, 43, 44, 45, 46, 47, 48, 49, 50]

注意:-

由于您希望第一个框 为空,因此您不能在此处使用Arrays.toString。您将不得不再使用一个循环,并以简单的方式打印您的数组。当您的索引为 [0][0] 时,只需 sysout("");

关于java - 试图制作一个双数组,编号为 2 到 50,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13383444/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com