现在我正在寻找用1和0来代表地形的围栏。 1将是围栏,0将是空格。这是我的代码
package assignment_2;
public class Fencing {
public static void main(String[] args) {
boolean b = true;
int i;
int j;
int[] [] map =
{
{0, 1, 1, 0},
{1, 2, 1, 1},
{1, 1, 4, 0},
{1, 1, 0, 0}
};
for (i = 0; i < 4; i++){
for (j = 0; j < 4; j++){
if (map[i][j] != 1 && map[i][j] != 0){
b = false;
if (b = false){
System.out.println("Map of the land owned:");
System.out.println(map[i][j]);
System.out.println("Map does not have the correct format");
System.out.println("--> A value of " + map[i][j] + " was found at " + i + "," + j);
}
else{
System.out.println("Map of the land owned: ");
System.out.println(map[i][j]);
System.out.println("The map is valid");
return;
}
}
}
}
}
}
我试图让代码做的是显示地图是否有效。如果地图由0或1以外的任何其他内容组成,则该地图无效,但由于某种原因,此代码使地图无效,无论内容是什么,并且正在显示第一个错误地图而不是打印出整个地图。我试图让最终结果看起来像这样:
Map of land owned
0110
1211
1141
1100
The map is not valid
A value of 2 was found at 1,1
A value of 4 was found at 2,2
现在看到的结果是什么
Map of the land owned:
2
The map is valid
由于某种原因似乎忽略了b boolean,有人知道如何解决它吗?
编辑:b问题已得到修复,这就是我现在正在做的事情
Map of the land owned:
2
Map does not have the correct format
--> A value of 2 was found at 1,1
答案 0 :(得分:2)
忽略whos
的原因是因为您使用的是赋值运算符,而不是条件运算符。这将始终评估为true,因为您正在分配值,因此将其设置为true。
将其更改为if (b = false)
或Balint指出if (b == false)
。
如果你想打印整张地图,你可以这样做:
(!b)