【问题标题】:Trying to make a fencing map using terrain representation尝试使用地形表示制作围栏地图
【发布时间】: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

【问题讨论】:

    标签: java eclipse terrain


    【解决方案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;
    }
    

    【讨论】:

    • 或者你知道,用!b
    • @Bálint 你也可以这样做,这取决于你的风格:)
    • 我不了解你,但这是我的顺序:更短的可读 > 更长的可读 > 更短的不可读 > 更长的不可读
    • 谢谢你的诀窍,它现在可以正确显示错误,但是你知道如何让它显示整个地图而不是第一个错误吗?
    • @MemeBoi 我更新了我的答案。我没有运行它,但你会为此做一些事情,打印出整个地图,然后确定它是否有效。我希望这会有所帮助。
    猜你喜欢
    • 2016-10-18
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-05-02
    • 2013-08-31
    • 2019-05-28
    相关资源
    最近更新 更多