尝试使用地形表示制作击剑地图
Trying to make a fencing map using terrain representation
现在我想用 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
它忽略 if (b = false)
的原因是因为您使用的是赋值运算符,而不是条件运算符。这将始终评估为真,因为您正在分配值,从而使其为真。
将其更改为 if (b == false)
或如 Balint 指出的 (!b)
。
如果你想打印出整张地图,你可以这样做:
int indexI = 0;
int indexJ = 0;
for (i = 0; i < 4; i++){
for (j = 0; j < 4; j++){
System.out.println("Map of the land owned:");
System.out.println(map[i][j]);
if (map[i][j] != 1 || map[i][j] != 0){
b = false;
indexI = i;
indexJ = j;
}
}
}
if (b == false){
System.out.println("Map does not have the correct format");
System.out.println("--> A value of " + map[indexI][indexJ] + " was found at " + indexI + "," + indexJ);
}
else{
System.out.println("Map of the land owned: ");
System.out.println("The map is valid");
return;
}
现在我想用 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
它忽略 if (b = false)
的原因是因为您使用的是赋值运算符,而不是条件运算符。这将始终评估为真,因为您正在分配值,从而使其为真。
将其更改为 if (b == false)
或如 Balint 指出的 (!b)
。
如果你想打印出整张地图,你可以这样做:
int indexI = 0;
int indexJ = 0;
for (i = 0; i < 4; i++){
for (j = 0; j < 4; j++){
System.out.println("Map of the land owned:");
System.out.println(map[i][j]);
if (map[i][j] != 1 || map[i][j] != 0){
b = false;
indexI = i;
indexJ = j;
}
}
}
if (b == false){
System.out.println("Map does not have the correct format");
System.out.println("--> A value of " + map[indexI][indexJ] + " was found at " + indexI + "," + indexJ);
}
else{
System.out.println("Map of the land owned: ");
System.out.println("The map is valid");
return;
}