繁体   English   中英

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

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

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

在用户键入quit之前,该程序无需永无休止。

所以我的问题是,什么会我把while(>> here <<) ,将尽快结束该程序在用户键入quit输入另一个字来代替。 单词quit不在数组中。

这是到目前为止,我的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)");
        }
    }   
}

如果您提出另一种方法来进行循环,虽然我很感激,但它不是while循环,但出于学校目的, 它必须是while循环

您的情况永远是正确的。 如果要在输入新的猜测时对用户输入"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++) {
        ...
    }
}

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

代码如下:

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

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

如果必须在输入后立即退出,并且必须使用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();
}

使用以下条件:

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

我的错,与“!=”搭配使用会更好

您期间的“ quit.compareTo(guess)!= 0”

暂无
暂无

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

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