简体   繁体   English

我如何退出这个 while 循环

[英]How do i exit this while loop

I'm trying to get this loop down but I don't know how to break from it.我正在尝试消除这个循环,但我不知道如何摆脱它。

System.out.printf("Please enter your given name and surname (Enter 0 to return to main menu)%n");
        String name = sc.nextLine();
        while (name.equals("0")) {
            System.out.printf(MENU_TEMPLATE);
            name = sc.nextLine();

if the user enters their name then the program will carry on as normal, but I'm having trouble doing this.如果用户输入他们的名字,那么程序将照常进行,但我在这样做时遇到了麻烦。

you use a conditional, when you want to break the loop.当您想中断循环时,您可以使用条件。 Then you use the break command.然后你使用break命令。

like喜欢

while (name != 0) {
   if (name == "Salami") {
     break;
   }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM