替代Java中的goto语句

替代Java中的goto语句

问题描述:

Java中 goto 关键字的替代功能是什么?

What is an alternative function for the goto keyword in Java?

由于Java没有goto。

Since Java does not have a goto.

您可以使用带标签的 BREAK 声明:

You could use a labeled BREAK statement:

search:
    for (i = 0; i < arrayOfInts.length; i++) {
        for (j = 0; j < arrayOfInts[i].length; j++) {
            if (arrayOfInts[i][j] == searchfor) {
                foundIt = true;
                break search;
            }
        }
    }

但是,在设计合理的代码中,你不应该需要GOTO功能。

However, in properly designed code, you shouldn't need GOTO functionality.