Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/327.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何在两个不同阵列的打印元素之间切换?_Java_Arrays_Printing - Fatal编程技术网

Java 如何在两个不同阵列的打印元素之间切换?

Java 如何在两个不同阵列的打印元素之间切换?,java,arrays,printing,Java,Arrays,Printing,我有两个二维阵列,都是3x3 我想打印出一个边框和中间的空格,如下所示: | | | | Note: 6 spaces " " per row / 3 spaces " " per cell | | 我这样做的思想过程是将2D数组视为坐标系,并将外边指定为等于1,以指示为边框[]打印边框“|”。然后,我会为每个单元格分配3个空格作为空格[]。然后,我将逐行执行边界[]和空格[]的交替打印语句 所以,一排一排地走就是了 print left border -

我有两个二维阵列,都是3x3

我想打印出一个边框和中间的空格,如下所示:

|      |
|      |    Note: 6 spaces " " per row / 3 spaces " " per cell 
|      |
我这样做的思想过程是将2D数组视为坐标系,并将外边指定为等于1,以指示为边框[]打印边框“|”。然后,我会为每个单元格分配3个空格作为空格[]。然后,我将逐行执行边界[]和空格[]的交替打印语句

所以,一排一排地走就是了

print left border -> print spaces -> don't print border -> print spaces -> print right border -> print spaces
或者进一步澄清一下我想做什么:

border[0][0] = 1 --> print left border "|"
space[0][0] = 1 --> print space "   "
border[0][1] = 0 --> DO NOT print border "|"
space[0][1] = 1 --> print space "   "
border[0][2] = 1 --> print border "|"
space[0][2] = 1 --> print space "   "
因此,第一行是:

 |      |
每行都是如此


所以我的问题是:

如何在两个不同阵列的打印元素之间切换


我试过了

    int border[][] = new int[3][3];
    int space[][] = new int[3][3];

    for(int i=0;i<border.length; i++){
        for(int j=0; j<space.length;j++){
            //Assigning Left and Right borders
            border[0][j] = 1;
            border[border.length-1][j] = 1;
            //Assigning spaces for every cell
            space[i][j] = 1;

            //Printing????
            if(border[i][j] == 1){
                System.out.print("|");
            }

            if(space[i][j] == 1){
                System.out.print("   ");
            }

        }
        System.out.println();
    }

我想我理解你。你遗漏了一些东西,比如else块,我建议你仔细阅读下面我的评论

int border[][] = new int[3][3];
int space[][] = new int[3][3];
for (int[] b : border) {  // <-- lets first create border arrays
  b[0] = b[2] = 1;        // <-- combine the 1s.
  b[1] = 0;              
}
for (int[] s : space) {   // <-- now space arrays
  s[0] = s[1] = s[2] = 1; // <-- combine the 1s.
}
// Now border and space are setup
for (int i = 0; i < border.length; i++) { 
  for (int j = 0; j < border[0].length; j++) {
    if(border[i][j] == 1) {
        System.out.print("|");
    } else { // <-- You need an else block
      System.out.print(" ");          
    }
    if(space[i][j] == 1){ // <-- space is pointless. This is always true.
        System.out.print("   ");
    }
  }
  System.out.println(); // <-- new line.
}

你的意思是像一个
if
的声明,我对此不清楚。你在储存边境?这不就像是在储藏甜甜圈吗?我并不是说要聪明,我不明白你为什么要存储空间以外的任何东西(假设空间是“物质”)。这是一个更大问题的一部分。从本质上说,我正在尝试制作一个网格。一个更真实的图片,你正在尝试做什么,会有所帮助。
int border[][] = new int[3][3];
int space[][] = new int[3][3];
for (int[] b : border) {  // <-- lets first create border arrays
  b[0] = b[2] = 1;        // <-- combine the 1s.
  b[1] = 0;              
}
for (int[] s : space) {   // <-- now space arrays
  s[0] = s[1] = s[2] = 1; // <-- combine the 1s.
}
// Now border and space are setup
for (int i = 0; i < border.length; i++) { 
  for (int j = 0; j < border[0].length; j++) {
    if(border[i][j] == 1) {
        System.out.print("|");
    } else { // <-- You need an else block
      System.out.print(" ");          
    }
    if(space[i][j] == 1){ // <-- space is pointless. This is always true.
        System.out.print("   ");
    }
  }
  System.out.println(); // <-- new line.
}
|       |   
|       |   
|       |