Java无法将值输入到二维数组中

时间:2021-10-17 21:37:10

I have this problem, I have to model a pipe with 1's and zeroes, the zeroes surround the pipe and the 1's are the pipe itself

我有这个问题,我必须建立一个1和0的管道,零管道围绕管道,1是管道本身

I am doing this using a 2D array, My code is as follows

我使用2D数组做这个,我的代码如下

for(int i = 0; i < x2; i++) { //x1 = 10, x2 = 20
        for(int j = 0; j < x2; j++) {
            if(i >= x2 - (x1+x2)/2 && j <= (x1+x2)/2) {
                pipeArray[i][j] = 1;
            } else {
                pipeArray[i][j] = 0;
            }
        }
    }

My output looks like

我的输出看起来像

 00000000000000000000
 00000000000000000000
 00000000000000000000
 00000000000000000000
 00000000000000000000
 11111111111111110000
 11111111111111110000
 11111111111111110000
 11111111111111110000
 11111111111111110000
 11111111111111110000
 11111111111111110000
 11111111111111110000
 11111111111111110000
 11111111111111110000
 11111111111111110000
 11111111111111110000
 11111111111111110000
 11111111111111110000
 11111111111111110000

And it needs to look like

它需要看起来像

 00000000000000000000
 00000000000000000000
 00000000000000000000
 00000000000000000000
 00000000000000000000
 00000111111111100000
 00000111111111100000
 00000111111111100000
 00000111111111100000
 00000111111111100000
 00000111111111100000
 00000111111111100000
 00000111111111100000
 00000111111111100000
 00000111111111100000
 00000000000000000000
 00000000000000000000
 00000000000000000000
 00000000000000000000
 00000000000000000000

6 个解决方案

#1


1  

Try this:

for(int i=0;i<x2;i++){
    for(int j=0;j<x2;j++){
        if(i > x2 - (x1+x2)/2 && j <= (x1+x2)/2 && i<=(x1+x2)/2 && j>(x2-(x1+x2)/2) {
            pipeArray[i][j] = 1;
        } else {
            pipeArray[i][j] = 0;
        }
    }
}

The problem was that you were only checking if it was lower or higher than one side. You didn't check if it was higher or lower than the other side, to see if it was in between both.

问题是你只是在检查它是否低于或高于一侧。您没有检查它是否高于或低于另一侧,看它是否在两者之间。

#2


0  

It's your conditional i >= x2 - (x1+x2)/2 && j <= (x1+x2)/2. You are saying "if the row is greater than five and the column is less than 15 then put a 1". What you need to say is "if the row is greater than five and less than 15 and if the column is greater than 5 and less than 15".

这是你的条件i> = x2 - (x1 + x2)/ 2 && j <=(x1 + x2)/ 2。你说的是“如果行大于5且列小于15则放1”。您需要说的是“如果行大于5且小于15且列大于5且小于15”。

#3


0  

You haven't really explained the rule properly, but regarding the wanted output I assume that you want a x2xx2 grid where the 1's are a x1xx1-block in the middle surrounded by 0's. In other words:

你没有真正正确地解释规则,但是对于想要的输出,我假设你想要一个x2xx2网格,其中1是一个x1xx1块,中间被0包围。换一种说法:

  • If the coordinates i and j are both larger than (x2-x1)/2 but smaller than or equal to x2-(x2-x1)/2, have a 1; otherwise have a 0.
  • 如果坐标i和j都大于(x2-x1)/ 2但小于或等于x2-(x2-x1)/ 2,则得1;否则有0。

It would be translated to this:

它将被翻译成:

for(int i = 0; i < x2; i++) {
    for(int j = 0; j < x2; j++) {
        if(i > (x2-x1)/2 && j > (x2-x1)/2 && i <= x2-(x2-x1)/2 && j <= x2-(x2-x1)/2) {
            pipeArray[i][j] = 1;
        } else {
            pipeArray[i][j] = 0;
        }
    }
}

#4


0  

You are missing two conditions : j >= (x2 - (x1+x2)/2) to restrict the 1s from the left. You have a condition for the right side. also i <= (x1+x2)/2 to restrict from the bottom.

您缺少两个条件:j> =(x2 - (x1 + x2)/ 2)以限制左侧的1。你有一个正确的条件。也是我<=(x1 + x2)/ 2从底部限制。

for(int i = 0; i < x2; i++) { //x1 = 10, x2 = 20
    for(int j = 0; j < x2; j++) {
        if(i >= x2 - (x1+x2)/2 && i <= (x1 + x2)/2 && j <= (x1+x2)/2 && j >= (x1 - (x1+x2)/2)) {
            pipeArray[i][j] = 1;
        } else {
            pipeArray[i][j] = 0;
        }
    }
}

#5


0  

There are two problems: First, the second condition has to be added, the second is that there are only 4 row/cols of 0 at the end, so you still to subtract, for example with (x1+x2-1)/2

有两个问题:第一,必须添加第二个条件,第二个是最后只有4个0行/ 0,所以你仍然要减去,例如用(x1 + x2-1)/ 2

    for(int i = 0; i < x2; i++) { //x1 = 10, x2 = 20
        for(int j = 0; j < x2; j++) {
            if(i >= x2 - (x1+x2)/2 && j <= (x1+x2-1)/2 && j >= x2 - (x1+x2)/2 && i <= (x1+x2-1)/2) {
                pipeArray[i][j] = 1;
            } else {
                pipeArray[i][j] = 0;
            }
        }
    }

#6


0  

For any pipe:

对于任何管道:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    int map[][] = new int[n][n];

    int x = n/2;
    int xx = x/2;
    int odd = n%2;

    for(int i=0;i<n;i++) {
        for(int j=0;j<n;j++) {
            if((i >= x - xx && i < x + xx + odd) && (j >= x - xx && j < x + xx + odd)) map[i][j] = 1;
            else map[i][j] = 0;
        }
    }

    for(int i=0;i<n;i++) {
        for(int j=0;j<n;j++) {
            System.out.print(map[i][j]);
        }
        System.out.println();
    }
}

#1


1  

Try this:

for(int i=0;i<x2;i++){
    for(int j=0;j<x2;j++){
        if(i > x2 - (x1+x2)/2 && j <= (x1+x2)/2 && i<=(x1+x2)/2 && j>(x2-(x1+x2)/2) {
            pipeArray[i][j] = 1;
        } else {
            pipeArray[i][j] = 0;
        }
    }
}

The problem was that you were only checking if it was lower or higher than one side. You didn't check if it was higher or lower than the other side, to see if it was in between both.

问题是你只是在检查它是否低于或高于一侧。您没有检查它是否高于或低于另一侧,看它是否在两者之间。

#2


0  

It's your conditional i >= x2 - (x1+x2)/2 && j <= (x1+x2)/2. You are saying "if the row is greater than five and the column is less than 15 then put a 1". What you need to say is "if the row is greater than five and less than 15 and if the column is greater than 5 and less than 15".

这是你的条件i> = x2 - (x1 + x2)/ 2 && j <=(x1 + x2)/ 2。你说的是“如果行大于5且列小于15则放1”。您需要说的是“如果行大于5且小于15且列大于5且小于15”。

#3


0  

You haven't really explained the rule properly, but regarding the wanted output I assume that you want a x2xx2 grid where the 1's are a x1xx1-block in the middle surrounded by 0's. In other words:

你没有真正正确地解释规则,但是对于想要的输出,我假设你想要一个x2xx2网格,其中1是一个x1xx1块,中间被0包围。换一种说法:

  • If the coordinates i and j are both larger than (x2-x1)/2 but smaller than or equal to x2-(x2-x1)/2, have a 1; otherwise have a 0.
  • 如果坐标i和j都大于(x2-x1)/ 2但小于或等于x2-(x2-x1)/ 2,则得1;否则有0。

It would be translated to this:

它将被翻译成:

for(int i = 0; i < x2; i++) {
    for(int j = 0; j < x2; j++) {
        if(i > (x2-x1)/2 && j > (x2-x1)/2 && i <= x2-(x2-x1)/2 && j <= x2-(x2-x1)/2) {
            pipeArray[i][j] = 1;
        } else {
            pipeArray[i][j] = 0;
        }
    }
}

#4


0  

You are missing two conditions : j >= (x2 - (x1+x2)/2) to restrict the 1s from the left. You have a condition for the right side. also i <= (x1+x2)/2 to restrict from the bottom.

您缺少两个条件:j> =(x2 - (x1 + x2)/ 2)以限制左侧的1。你有一个正确的条件。也是我<=(x1 + x2)/ 2从底部限制。

for(int i = 0; i < x2; i++) { //x1 = 10, x2 = 20
    for(int j = 0; j < x2; j++) {
        if(i >= x2 - (x1+x2)/2 && i <= (x1 + x2)/2 && j <= (x1+x2)/2 && j >= (x1 - (x1+x2)/2)) {
            pipeArray[i][j] = 1;
        } else {
            pipeArray[i][j] = 0;
        }
    }
}

#5


0  

There are two problems: First, the second condition has to be added, the second is that there are only 4 row/cols of 0 at the end, so you still to subtract, for example with (x1+x2-1)/2

有两个问题:第一,必须添加第二个条件,第二个是最后只有4个0行/ 0,所以你仍然要减去,例如用(x1 + x2-1)/ 2

    for(int i = 0; i < x2; i++) { //x1 = 10, x2 = 20
        for(int j = 0; j < x2; j++) {
            if(i >= x2 - (x1+x2)/2 && j <= (x1+x2-1)/2 && j >= x2 - (x1+x2)/2 && i <= (x1+x2-1)/2) {
                pipeArray[i][j] = 1;
            } else {
                pipeArray[i][j] = 0;
            }
        }
    }

#6


0  

For any pipe:

对于任何管道:

public static void main(String[] args) {
    Scanner scan = new Scanner(System.in);
    int n = scan.nextInt();
    int map[][] = new int[n][n];

    int x = n/2;
    int xx = x/2;
    int odd = n%2;

    for(int i=0;i<n;i++) {
        for(int j=0;j<n;j++) {
            if((i >= x - xx && i < x + xx + odd) && (j >= x - xx && j < x + xx + odd)) map[i][j] = 1;
            else map[i][j] = 0;
        }
    }

    for(int i=0;i<n;i++) {
        for(int j=0;j<n;j++) {
            System.out.print(map[i][j]);
        }
        System.out.println();
    }
}