[英]My simple array program not giving the expected output
I wrote a simple array which I store 3x3 matrix. 我写了一个简单的数组,存储3x3矩阵。 But when I run the code it doesn't give 3x3 matrix.
但是,当我运行代码时,它不会给出3x3矩阵。 just gives a single column as output.
只给出一列作为输出。
class sucks
{
public static void main(String[] args)
{
int g[][]=new int[3][3];
int h,k,l=0;
for(k=0;k<3;k++)
{
for(h=0;h<3;h++)
{
g[k][h]=l;
l++;
}
}
for(k=0;k<3;k++)
{
for(h=0;h<3;h++)
{
System.out.print(g[k][h]+" ");
System.out.println();
}
}
}
}
The out put is like this 输出是这样的
0 0
1 1
2 2
3 3
4 4
5 五
6 6
7 7
8 8
Just print a new line for each row. 只需为每行打印一行。 Like this :
像这样 :
for(k=0;k<3;k++){
for(h=0;h<3;h++){
System.out.print(g[k][h]+" ");
}
System.out.println();
}
To improve your code, you could also change your for loops like this : 为了改善您的代码,您还可以像下面这样更改for循环:
for(k=0;k<g.length;k++){
for(h=0;h<g[k].length;h++){
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.