英文:
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 = "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();
}
}
}
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("");
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();
}
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论