如果给定条件为真,那么做一些事情。如果是假,那么再试一次

时间:2014-08-08 17:04:11

标签: java if-statement return conditional-statements

我正在为一个游戏编写一个脚本,我希望我的角色可以走一定数量的瓷砖,但我无法让它发挥作用。所以我想寻找瓷砖,如果没有,我不希望阵列继续,但返回并重试。

public void printTile() {

    // Set length of the tile array. (i.e. how much tiles you need to interact with).
    Tile[] tileArray = new Tile[11];

    // Enter desired tiles in here, last digit is the plan which is standard 0.
    tileArray[0] = new Tile(2683, 3275, 0); 
    tileArray[1] = new Tile(2670, 3277, 0);
    tileArray[2] = new Tile(2657, 3276, 0);
    tileArray[3] = new Tile(2645, 3283, 0);
    tileArray[4] = new Tile(2635, 3289, 0);
    tileArray[5] = new Tile(2625, 3296, 0);
    tileArray[6] = new Tile(2617, 3297, 0);
    tileArray[7] = new Tile(2608, 3296, 0);
    tileArray[8] = new Tile(2598, 3297, 0);
    tileArray[9] = new Tile(2587, 3296, 0);
    tileArray[10] = new Tile(2577, 3298, 0);

    // Declare i, so we can count up the amount of tiles in this array.
    int i;

    // Self-explatanory. Array starts at 0. If i < amount of tiles in our array execute. Count up i every time, else it repeats the first tile.
    for(i = 0; i < tileArray.length; i++) {
        log("Walking to: " + tileArray[i]); 
        Time.sleep(1000); // Sleep is good for brain.

        if(Walking.canReach(tileArray[i])) {
            if(!Players.getLocal().isMoving() || Walking.getRealDistanceTo(tileArray[i]) < 2) {
                if(Walking.walkTileMM(tileArray[i])) {
                    Time.sleep(1000, 1500);

                    while(Walking.getDestinationDistance() > 1) {
                        Time.sleep(500);
                    }
                } else {
                    Time.sleep(700);
                    return;
                }
            }
        } else {
            Time.sleep(500);
            return;
        }
    }
}

1 个答案:

答案 0 :(得分:4)

您可能希望在continue子句中使用return而不是elsecontinue将跳转到循环的下一次迭代,而不是退出方法。