【发布时间】:2016-10-25 16:44:06
【问题描述】:
现在我想用 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 布尔值,有人知道如何修复它吗?
编辑:b 问题已经修复,这就是我现在得到的
Map of the land owned:
2
Map does not have the correct format
--> A value of 2 was found at 1,1
【问题讨论】: