How do I go back to a specific line in Java?

I'm writing code that involves if else statements to ask the user if they want to continue I don't know how to do this in Java Is there a label like I can use?

This is 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 I help you?

Solution

Java has no goto statement (although the goto keyword is reserved) The only way to return code in Java is to use loops When you want to exit the loop, use break; To return to the loop title, continue using

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;
    }
}
The content of this article comes from the network collection of netizens. It is used as a learning reference. The copyright belongs to the original author.
THE END
分享
二维码
< <上一篇
下一篇>>