簡體   English   中英

Java:for循環忽略條件

[英]Java: for loop ignoring conditions

我的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

您需要“和”,而不是“或”。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM