建立了两组数列
for (int row = 0; row < board_size; row++) {
            for (int col = 0; col < board_size; col++) {
                System.out.print(theBoard[row][col]);                  
            }
            System.out.println();
        }
我现在需要把这两组数列以如下形式展现出来,是两个只有3个数字的数列,应该怎么样做? | |
-----
 | |
_____
 | | 
-----

解决方案 »

  1.   

    这是我自己写的,我看和你的差不多,你看看:
    import java.util.Arrays;
    public class TwoArrays
    {
    public static void main(String args[])
    {
    int arr[][] = new int[][]{{1,2,3},{4,5,6}};
    for(int i = 0;i < arr.length;i++)
    {
    for(int j = 0;j < arr[i].length;j++)
    System.out.print(arr[i][j]);
    System.out.println();
    }
    }
    }
      

  2.   

    哦,这个和你的一样了~
    import java.util.Arrays;
    public class TwoArrays
    {
    public static void main(String args[])
    {
    int arr[][] = new int[][]{{1,2},{3,4},{5,6}};
    for(int i = 0;i < arr.length;i++)
    {
    for(int j = 0;j < arr[i].length;j++)
    System.out.print(arr[i][j]);
    System.out.println();
    }
    }
    }
      

  3.   

    我在解释一下,麻烦大家再帮帮忙,就差这个问题了
    建立了一个2d的array
    for (int row = 0; row < board_size; row++) {
      for (int col = 0; col < board_size; col++) {
      System.out.print(theBoard[row][col]);   
      }
      System.out.println();
      }
    通过上述代码我可以把这个2d array都一个个显示出来,但是我这个是是两个3个元素的array(看的英文版教材。。)画出图来就是3X3的正方形,我想通过System.out.println这个方法把这几个元素显示出来,格式如下(x代表array里的数字)
    x|x|x
    -----
    x|x|x
    -----
    x|x|x
    这样解释会不会好一些?
      

  4.   

    通过上述代码我可以把这个2d array都一个个显示出来,但是这个是是两个3个元素的array(看的英文版教材。。)“但是”啥意思,为什么还是转折???
    什么叫“是是两个3个元素的array”
    “我想通过System.out.println这个方法把这几个元素显示出来”--- 你的意思是打印下面这样的数字?x|x|x
    -----
    x|x|x
    -----
    x|x|x总之,我晕了
      

  5.   

    啊就是2d的array
    比如
    横着0,1,2
    竖着0,1,2
    那最后一共就是9个数字,我想通过那样的方法把9个数字一次打印出来,这样说会不会好一点
      

  6.   

    还是不明白,你的是一个3*3二维数组?array[3][3]这样的数组?
    想打成一个3*3的正方形?
      

  7.   

    array[3][3]和二维数组不一样么?
    想打成3*3的正方形。。
      

  8.   

    对!!!!!!!!!!!!!!!!!!!!
    太对了!!!!!!!!!!!!!!!!!!!
    用'|'和----把这6个隔离开来,为了更方便阅读!!!!!!!!!!!!!!!!
    终于有明白人了激动的我真是内牛满面
    高人你有办法吗????????就用system.out.println做出这种格式,像我前面贴的似的,有两个for分别对应那个正方形的两个坐标。。希望这次不要出现理解上的差错了
    for (int row = 0; row < board_size; row++) {
      for (int col = 0; col < board_size; col++) {
      System.out.print(theBoard[row][col]);   
      }
      System.out.println();
      }
    我现在改成这样了,显示结果很怪异,麻烦高人了。。
     for (int row = 0; row < board_size; row++) {
                for (int col = 0; col < board_size; col++) {
                    System.out.print(theBoard[row][col]);                  
                    if (col==0){System.out.print('|');}
                    if (col==1){System.out.print('|'+"\n");}
                    if (col==2&&row!=2){System.out.print("-----"+"\n");}
                    
                }
                
            }
      

  9.   

    public class Test {
    public static void main(String[] args) {
    int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    Test.printArray(a);
    } public static void printArray(int[][] a) {
    for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a[i].length; j++) {
    if (j != a[i].length - 1) {
    System.out.print(a[i][j] + " | ");
    } else {
    System.out.print(a[i][j]);
    }
    }
    System.out.println();
    System.out.println("----------");
    }
    }
    }
      

  10.   

    public class Test {
    public static void main(String[] args) {
    int[][] a = { { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } };
    Test.printArray(a);
    } public static void printArray(int[][] a) {
    for (int i = 0; i < a.length; i++) {
    for (int j = 0; j < a[i].length; j++) {
    if (j != a[i].length - 1) {
    System.out.print(a[i][j] + " | ");
    } else {
    System.out.print(a[i][j]);
    }
    }
    System.out.println();
    if (i != a.length - 1) {
    System.out.println("----------");
    }
    }
    }
    }
    这个不会打印最后一行---------------
      

  11.   

    恩。。谢谢poemlover,但是我知道可能如下的话听起来很奇怪,但是'|'和----都没打出来
      

  12.   

    啊。。我的错,搞定了!在此感谢poemlover