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

时间:2014-10-29 23:39:42

标签: java loops if-statement for-loop while-loop

我正在编写一个拼写检查程序,它会向用户询问一个单词,并将其与我所创建的数组中的单词列表进行比较。

在用户输入quit之前,该程序必须永无止境。

所以我的问题是,一旦用户输入while(>> here <<)而不是输入另一个单词,我会将quit放在哪个程序结束程序。单词quit不在数组中。

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

如果你建议采用另一种方式,这不是一个循环,虽然我很感激,它必须是一个循环用于学校目的。

6 个答案:

答案 0 :(得分:2)

你的情况总是如此。如果您想在输入新猜测时对输入"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++) {
        ...
    }
}

答案 1 :(得分:0)

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

答案 2 :(得分:0)

你的循环将一直持续到你将String退出改为&#34;退出&#34;以外的其他东西。这是一个例子:

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

答案 3 :(得分:0)

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

答案 4 :(得分:0)

使用以下条件:

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

答案 5 :(得分:-1)

我错了,用“!=”

会更好

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