简体   繁体   English

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

[英]Quit a While Loop if the user types in QUIT

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 quit instead of typing another word. 所以我的问题是,什么会我把while(>> here <<) ,将尽快结束该程序在用户键入quit输入另一个字来代替。 The word quit is not in the array. 单词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循环代码(尚未完成,但是当我对此进行测试时,它使我感到烦恼,并且循环并没有以quit结尾,所以这是我首先需要执行的操作)。

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 loop for school purposes. 如果您提出另一种方法来进行循环,虽然我很感激,但它不是while循环,但出于学校目的, 它必须是while循环

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"做出反应,可以使用无限循环和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++) {
        ...
    }
}

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"));

Your loop while will keep going until you change your String quit to something other than "quit". 在将String quit更改为“ quit”以外的内容之前,您的循环将继续进行。 Here is an example: 这是一个例子:

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)");
        }
}   
}

If you must quit right after input, and you must use a while condition 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. 如果必须在输入后立即退出,并且必须使用while条件来触发它,则您的输入必须在循环的末尾,以便在输入后立即检查该条件。 You could do that like this: 您可以这样做:

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();
}

Use the below Condition : 使用以下条件:

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

My wrong, it will work better with "!=" 我的错,与“!=”搭配使用会更好

"quit.compareTo(guess)!=0" in your while 您期间的“ quit.compareTo(guess)!= 0”

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

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