goto关键字在Java中没有对应的替代函数。
goto关键字在Java中没有对应的替代函数。
使用标记的break作为goto的替代方案。
goto
,因为它会使代码变得不结构化和不清晰。但是,你可以使用break
和continue
作为goto的文明形式,而不会出现它的问题。
ahead: {
System.out.println("Before break");
break ahead;
System.out.println("After Break"); // This won't execute
}
// After a line break ahead, the code flow starts from here, after the ahead block
System.out.println("After ahead");
输出:
Before Break
After ahead
before: {
System.out.println("Continue");
continue before;
}
这将导致一个无限循环,因为每次执行 continue before
这一行时,代码流将从 before
开始重新执行。