2D数组中的索引值与另一个2D数组中的相同值不相等。

huangapple go评论72阅读模式
英文:

Value of index in 2d array not equal to same value of other 2d array

问题

以下是您提供的代码部分的中文翻译:

尝试在a[i][j] == b[i][j]且a[i][j]= b[i][j]创建一个n*n的+-模式不确定为什么这不输出预期结果

如果我循环遍历a[i][j] == seq[i]输出相同尝试在比较之前将a[i][j]和seq[i]分配给变量结果相同

public class ThueMorse {
    public static void main(String[] args) {
        int n = Integer.parseInt(args[0]);
        String thue   = "0";
        String morse  = "1";
        String[][] a = new String[n][n];
        String[][] b = new String[n][n];
        

        for (int i = 1; i <= n; i++) {
            String t = thue;
            String m = morse;
            thue  += m;
            morse += t;
            if (thue.length() >= n) break;
        }
        String [] seq = new String[n];
        seq = thue.split("");
        for (int i = 0; i < n; i ++) {
            for (int j = 0; j < n; j ++) {
                a[i][j] = seq[j];
                b[i][j] = seq[i];
                
                if (j != 0) System.out.print(" ");
                if (a[i][j] == b[i][j]) {
                System.out.print("+");
                }
                else {
                System.out.print("-");
                }

                if (j != n) System.out.print(" ");
            }    
            System.out.println();
        }
    }
}

请注意,此处只提供代码的中文翻译,不包括问题的回答。

英文:

Trying to make an n*n pattern of "+" and "-" when value a[i][j] == b[i][j] and value a[i][j] != b[i][j]. Unsure of why this isn't outputting expected.

Same output if I loop through a[i][j] == seq[i].
Tried assigning a[i][j] and seq[i] to variables before comparing and same result.

public class ThueMorse {
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
String thue   = &quot;0&quot;;
String morse  = &quot;1&quot;;
String[][] a = new String[n][n];
String[][] b = new String[n][n];
for (int i = 1; i &lt;= n; i++) {
String t = thue;
String m = morse;
thue  += m;
morse += t;
if (thue.length() &gt;= n) break;
}
String [] seq = new String[n];
seq = thue.split(&quot;&quot;);
for (int i = 0; i &lt; n; i ++) {
for (int j = 0; j &lt; n; j ++) {
a[i][j] = seq[j];
b[i][j] = seq[i];
if (j != 0) System.out.print(&quot; &quot;);
if (a[i][j] == b[i][j]) {
System.out.print(&quot;+&quot;);
}
else {
System.out.print(&quot;-&quot;);
}
if (j != n) System.out.print(&quot; &quot;);
}    
System.out.println();
}
}
}

For reference:

seq[] = [0, 1, 1, 0, 1, 0, 0, 1]
a[i][j] =
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
0  1  1  0  1  0  0  1
b[i][j] =
0  0  0  0  0  0  0  0
1  1  1  1  1  1  1  1
1  1  1  1  1  1  1  1
0  0  0  0  0  0  0  0
1  1  1  1  1  1  1  1
0  0  0  0  0  0  0  0
0  0  0  0  0  0  0  0
1  1  1  1  1  1  1  1

Output:

+  -  -  -  -  -  -  -
-  +  -  -  -  -  -  -
-  -  +  -  -  -  -  -
-  -  -  +  -  -  -  -
-  -  -  -  +  -  -  -
-  -  -  -  -  +  -  -
-  -  -  -  -  -  +  -
-  -  -  -  -  -  -  +

Expected:

+  -  -  +  -  +  +  -
-  +  +  -  +  -  -  +
-  +  +  -  +  -  -  +
+  -  -  +  -  +  +  -
-  +  +  -  +  -  -  +
+  -  -  +  -  +  +  -
+  -  -  +  -  +  +  -
-  +  +  -  +  -  -  +

答案1

得分: 3

问题出在b数组的初始化部分。

在嵌套的for循环中,您在为b赋值时,使用了seq[j]而不是seq[i]作为每个元素的值。

要解决此问题,请将以下行更改为:

b[i][j] = seq[j];

一旦您进行此更改,代码应该产生预期的输出。

英文:

The issue with the code seems to be in the initialization of the b array.

In the nested for loop where you are assigning values to b, you are using seq[j] instead of seq[i] as the value for each element.

To fix this, change the line

b[j][i] = seq[j];

to

b[i][j] = seq[j];

Once you make this change, the code should produce the expected output.

答案2

得分: 1

.split() 导致了问题。将序列中的值转换为新的整数数组解决了问题。

String[] seq = new String[n];
seq = thue.split("");
    
int[] num = new int[n]; 

for (int i = 0; i < n; i++) {
    num[i] = Integer.parseInt(seq[i]);
}
for (int i = 0; i < n; i++) {
    for (int j = 0; j < n; j++) {
        a[i][j] = num[j];
        b[i][j] = num[i];
        if (j != 0) System.out.print(" ");
        if (a[i][j] == b[i][j]) {
            System.out.print("+");
        }
        else {
            System.out.print("-");
        }

        if (j != n) System.out.print(" ");
    }
    System.out.println();
}
英文:

Turns out .split() was causing the issue. Converting the values from seq into a new array of type int fixed the problem.

    String [] seq = new String[n];
seq = thue.split(&quot;&quot;);
int[] num = new int[n]; 
for (int i = 0; i &lt; n; i++) {
num[i] = Integer.parseInt(seq[i]);
}
for (int i = 0; i &lt; n; i ++) {
for (int j = 0; j &lt; n; j ++) {
a[i][j] = num[j];
b[i][j] = num[i];
if (j != 0) System.out.print(&quot; &quot;);
if (a[i][j] == b[i][j]) {
System.out.print(&quot;+&quot;);
}
else {
System.out.print(&quot;-&quot;);
}
if (j != n) System.out.print(&quot; &quot;);
}
System.out.println();
}

huangapple
  • 本文由 发表于 2023年4月19日 15:53:18
  • 转载请务必保留本文链接:https://go.coder-hub.com/76051975.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定