I'm writing code that involves an if-else statement asking the user if they want to continue. I have no idea how to do this in Java. Is there like a label I can use for this?
This is kind of what I'm looking for:
--label of some sort--
System.out.println("Do you want to continue? Y/N");
if (answer=='Y')
{
goto suchandsuch;
}
else
{
System.out.println("Goodbye!");
}
Can anybody help?
Java has no goto
statement (although the goto
keyword is among the reserved words). The only way in Java to go back in code is using loops. When you wish to exit the loop, use break
; to go back to the loop's header, use 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;
}
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With