Java:for循环忽略条件

标签 java android

我的 for 循环忽略了 !found 字段,即使找到 == true 也会继续执行:

public int getDownYEntities() {
    int loc = 16;
    boolean found = false;
    EntityType eCheck;
    for (int col = locY; col < 17 || !found; col++) {
        eCheck = level[locX][col];
        System.out.println("Called");
        if ((eCheck == EntityType.ROCK) || (eCheck == EntityType.BOULDER) || (eCheck == EntityType.KEY) || (eCheck == EntityType.EXIT)) {
            switch (eCheck) {
                case ROCK:
                    loc = col - 2;
                    found = true;
                    break;
                case BOULDER:
                    loc = col - 2;
                    found = true;
                    System.out.println("Test1");
                    System.out.println(found);
                    break;
                case KEY:
                    loc = col - 2;
                    found = true;
                    break;
                case EXIT:
                    loc = col - 1;
                    found = true;
                    break;
            }
        }
        else
        {
            loc = col-1;
        }
    }
    return loc;
}

日志:

 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Test1
 I/System.out: true
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called
 I/System.out: Called

我是个彻头彻尾的白痴吗?

最佳答案

col < 17 && !found

你想要“和”,而不是“或”。

关于Java:for循环忽略条件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35639630/

相关文章:

java - retrofit /Sphere 引擎 API 错误 : undefined reference to `main'

java - Spring Factory 创建监听队列的同步消费者

java - Android lib Admob 没有足够的空间来显示平板电脑广告

java - 从图库中选择图像后 Android 应用程序崩溃 -cursor.getColumnIndex 返回 -1

android - 单击应用程序图标打开抽屉导航

android - 如何使 View 逐渐出现,而不是突然出现

java - JBig2在运行时无法解析?

java - 从服务器获取 505 响应

android - Kotlin Coroutines 在 Android 中的使用

android - Eclipse 模拟器中的 Google map 未出现