我正在编写包含 if-else 语句的代码,询问用户是否要继续。我不知道如何在 Java 中执行此操作。有没有我可以使用的标签?
这就是我要找的东西:
--label of some sort--
System.out.println("Do you want to continue? Y/N");
if (answer=='Y')
{
goto suchandsuch;
}
else
{
System.out.println("Goodbye!");
}
有人可以帮忙吗?
最佳答案
Java 没有goto
语句(尽管goto
关键字在保留字中)。在 Java 中返回代码的唯一方法是使用循环。当你想退出循环时,使用 break
;要返回到循环的标题,请使用 continue
。
while (true) {
// Do something useful here...
...
System.out.println("Do you want to continue? Y/N");
// Get input here.
if (answer=='Y') {
continue;
} else {
System.out.println("Goodbye!");
break;
}
}
关于java - 如何返回到Java中的特定行?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18604169/