KTF*_*KTF 3 java label loops if-statement
我正在编写涉及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!");
}
Run Code Online (Sandbox Code Playgroud)
有人可以帮忙吗?
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;
}
}
Run Code Online (Sandbox Code Playgroud)