我对此感到困惑。我们大多数人都听说过Java中没有goto语句。
但我发现它是Java中的关键字之一。它可以在哪里使用?如果它不能使用,那么为什么它作为关键字包含在Java中?
我对此感到困惑。我们大多数人都听说过Java中没有goto语句。
但我发现它是Java中的关键字之一。它可以在哪里使用?如果它不能使用,那么为什么它作为关键字包含在Java中?
当前回答
goto不在Java中
你必须使用GOTO 但它不能正常工作。在关键的Java词中,它没有被使用。 http://docs.oracle.com/javase/tutorial/java/nutsandbolts/_keywords.html
public static void main(String[] args) {
GOTO me;
//code;
me:
//code;
}
}
其他回答
禁止声明同名变量。
如。 Int I = 0, goto;
它被认为是不能做的事情之一,但可能被列为保留词,以避免开发人员感到困惑。
请参阅下面的链接,它显示了所有java保留字,并告诉您添加了哪些版本。
http://java.sun.com/docs/books/tutorial/java/nutsandbolts/_keywords.html
Goto是保留的,即使目前没有使用,但永远不要说永远:)
不,没有使用goto,但是您可以定义标签并留下一个到标签的循环。您可以使用break或continue后跟标签。所以你可以跳出多个循环层。看看教程。
是的,这是可能的,但不像在c#(在我看来,c#更好!)那些总是模糊软件的观点是愚蠢的!很遗憾java没有至少goto case xxx。
跳转向前:
public static void main(String [] args) {
myblock: {
System.out.println("Hello");
if (some_condition)
break myblock;
System.out.println("Nice day");
}
// here code continue after performing break myblock
System.out.println("And work");
}
向后跳:
public static void main(String [] args) {
mystart: //here code continue after performing continue mystart
do {
System.out.println("Hello");
if (some_condition)
continue mystart;
System.out.println("Nice day");
} while (false);
System.out.println("And work");
}