gpt4 book ai didi

java - 如何让二维数组打印 1 到 80

转载 作者:行者123 更新时间:2023-11-29 03:09:38 24 4
gpt4 key购买 nike

我的程序的目标是打印一个从 1 到 80 的二维数组。我试图让它看起来像这样。

 1   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
51 52 53 54 55 56 57 58 59 60
61 62 63 64 65 66 67 68 69 70
71 72 73 74 75 76 77 78 79 80

我可以稍后修复格式和所有内容,但如何让二维数组打印 1-80 的数字?我附上了我到目前为止所做的代码。

公开课转换 {

    public static void main (String[] args)
{

int[][] twoarray = new int[8][10];

int i ;
int j ;

for(i =0; i < 8; i++)
{
for(j = 0; j < 10; j++)
twoarray[i][j] = (i * j);
}


for(i = 0; i < 8; i++)
{
for(j = 0; j < 10; j++)
{

System.out.print(i);
System.out.print(" ");
System.out.print(j);
System.out.print(" ");


}
System.out.println();
}
}
}

最佳答案

你没有正确地初始化数组

twoarray[i][j] = (i * j);

应该是

twoarray[i][j] = (i * 10 + j + 1);

附带说明一下,这与计算屏幕上像素的内存地址(内存偏移量)的方式非常相似,给定它的 X 和 Y 坐标以及屏幕宽度。

然后实际使用二维数组

for(i = 0; i < 8; i++)
{
for(j = 0; j < 10; j++)
{
System.out.print(twoarray[i][j]);
}
}

关于java - 如何让二维数组打印 1 到 80,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/30114027/

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