三维数组的JAVA问题 [英] JAVA problem of three dimensional array

查看:115
本文介绍了三维数组的JAVA问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

亲爱的Web开发人员,

我的以下JAVA代码有问题:

Dear web developers,

I have problem with the following JAVA code:

class threeMatrix{

  public static void main(String[] args){

    int threeD[][][] = new int[3][4][5];

    int i,j,k;

    for(i=0;i<3;i++){

      for(j=0;j<4;j++){

	for(k=0;k<5;k++){

	  threeD[i][j][k] = i*j*k;

	  System.out.print(threeD[i][j][k]);

	}

	System.out.println();

      }

      System.out.println();

    }

  }

}



输出:



the output:

0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
0 0 0 0 0
 
0 0 0 0 0
0 1 2 3 4
0 2 4 6 8
0 3 6 9 12

0 0 0 0 0
0 2 4 6 8
0 4 8 12 16
0 6 12 18 24 



但我希望输出为:



But I want the output to be:

0 0 0 0 0	 0 0 0 0 0	 0 0 0 0 0
0 0 0 0 0	 0 1 2 3 4	 0 2 4 6 8
0 0 0 0 0	 0 2 4 6 8	 0 4 8 12 16
0 0 0 0 0	 0 3 6 9 12	 0 6 12 18 24

推荐答案

这很琐碎,因此我将代码留给您.

首先,应该交换前两个for循环:
It is very trivial, so I''ll leave the code to you.

First, the first two for loops should be swapped:
for (j=0;j<4;j++) {
    for (i=0;i<3;i++) {
        for (k=0;k<5;k++) {
           // code
        }
    }
}



然后只是在合适的位置获取空间,制表符和换行符的一种情况.
如果您首先学习以正确的顺序获取数据,然后再担心要使用它,这总是很容易的.



Then it''s just a case of getting your space, tab and linefeed at the right point.
It will always be easier if you learn to get the data in the right order first, then worry about what you are going to do with it.


这是您的意思吗?
Do you mean this?
public static void main(String[] args){
 
    int threeD[][][] = new int[3][4][5];
 
    int i,j,k;
 
    for(j=0;j<4;j++){
      for(i=0;i<3;i++){
	for(k=0;k<5;k++){
	  threeD[i][j][k] = i*j*k; 
          System.out.print(threeD[i][j][k]);
          System.out.print(" ");
	} 
        System.out.print("      ");
      }
      System.out.println("");
    } 
  }
}


这篇关于三维数组的JAVA问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆