作者热门文章
- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我的程序的目标是打印一个从 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/
我是一名优秀的程序员,十分优秀!