java 如果用户输入 QUIT,则退出 While 循环

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/26642544/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 10:18:36  来源:igfitidea点击:

Quit a While Loop if the user types in QUIT

javaloopsif-statementfor-loopwhile-loop

提问by Alex Cooper

I'm writing a spellchecker program that will ask the user for a word and will compare it to a list of words in an array that I have made.

我正在编写一个拼写检查程序,它将要求用户输入一个单词,并将其与我制作的数组中的单词列表进行比较。

This program needs to be never ending until the user types quit.

该程序需要在用户输入之前永不结束quit

So my question is, what would I put while(>> here <<)that would end the program as soon as the user types quitinstead of typing another word. The word quitis not in the array.

所以我的问题是,while(>> here <<)当用户quit输入而不是输入另一个词时,我会放什么来结束程序。这个词quit不在数组中。

Here is my code for the while loop so far (not done yet, but it bugs me when I test this and the loop doesn't end on quit, so that's what I need done first).

到目前为止,这是我的 while 循环代码(还没有完成,但是当我测试这个时它让我感到烦恼,并且循环没有在退出时结束,所以这就是我需要首先完成的)。

String quit = "quit"; 
//this is only declared as "quit" to get the loop to run for testing

while(quit.equals("quit")) {

    System.out.println("Please every a word or QUIT to exit");
    String guess = input.nextLine();

    for(int i = 0; i < words.length; i++) {

        if(guess.equals(words[i])) {

            System.out.println("That word is spelled correctly.");
        }   

        else {

            System.out.println("That word is not spelled correctly.");
            System.out.println("Would you like to add it to the dictionary? (Y/N)");
        }
    }   
}

If you suggest another way of doing it that's not a while loop, although I appreciate it, it has to be a while loopfor school purposes.

如果您建议另一种不是 while 循环的方法,尽管我很欣赏它,出于学校目的,它必须是一个 while 循环

回答by dasblinkenlight

Your condition is always true. If you want to react to a user typing in "quit"while entering a new guess, you can do it with an infinite loop and a break, like this:

你的条件总是正确的。如果您想对"quit"输入新猜测的用户做出反应,您可以使用无限循环和 a 来完成break,如下所示:

while (true) {
    System.out.println("Please every a word or QUIT to exit");
    String guess = input.nextLine();
    if (quit.equalsIgnoreCase(guess)) {
        break;
    }
    for(int i = 0; i < words.length; i++) {
        ...
    }
}

回答by aaaaaaaaaaaaaaaaaaaaaaaaaaaaaa

Use Do-While loop instead of simple While loop.

使用 Do-While 循环而不是简单的 While 循环。

The code looks like this:

代码如下所示:

String quit = "quit"; 
//this is only declared as "quit" to get the loop to run for testing

    do {

        System.out.println("Please every a word or QUIT to exit");
        String guess = input.nextLine();

        for(int i = 0; i < words.length; i++) {

            if(guess.equals(words[i])) {

                System.out.println("That word is spelled correctly.");
            }   

            else {

                System.out.println("That word is not spelled correctly.");
                System.out.println("Would you like to add it to the dictionary? (Y/N)");
            }
        }   
    }

    while(!quit.equals("quit"));

回答by Evald

Your loop while will keep going until you change your String quit to something other than "quit". Here is an example:

您的 while 循环将继续进行,直到您将 String quit 更改为“quit”以外的其他内容。下面是一个例子:

while(quit.equals("quit")) {

    System.out.println("Please every a word or QUIT to exit");
    String guess = input.nextLine();

    for(int i = 0; i < words.length; i++) 
    {

        if(guess.equals(words[i])) 
        {
            System.out.println("That word is spelled correctly.");
        } 
        if(guess.equals("quit") {
            quit = "something_else";
        }  
        else
        {
            System.out.println("That word is not spelled correctly.");
            System.out.println("Would you like to add it to the dictionary? (Y/N)");
        }
}   
}

回答by khelwood

If you must quit right after input, and you must use a whilecondition to trigger it, then your input has to be at the end of the loop so that the condition is checked right after the input is entered. You could do that like this:

如果您必须在输入后立即退出,并且必须使用while条件来触发它,那么您的输入必须位于循环的末尾,以便在输入输入后立即检查条件。你可以这样做:

System.out.println("Please enter a word or QUIT to exit");
String guess = input.nextLine();

while (!guess.equalsIgnoreCase("quit")) {
    for(int i = 0; i < words.length; i++) {
        if(guess.equals(words[i])) {
            System.out.println("That word is spelled correctly.");
        } else {
            System.out.println("That word is not spelled correctly.");
            System.out.println("Would you like to add it to the dictionary? (Y/N)");
        }
    }
    System.out.println("Please enter a word or QUIT to exit");
    String guess = input.nextLine();
}

回答by MaheshB

Use the below Condition :

使用以下条件:

if(StringUtils.equalsIgnoreCase(guess,"QUIT"))  {
    System.out.println("Quitting the loop");
    return
}

回答by Bamb Bakang Tonjé

My wrong, it will work better with "!="

我的错,它会用“!=”更好地工作

"quit.compareTo(guess)!=0" in your while

“quit.compareTo(guess)!=0” 在你的时候