【问题标题】:Quit a While Loop if the user types in QUIT如果用户输入 QUIT,则退出 While 循环
【发布时间】:2014-10-29 23:39:42
【问题描述】:

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

在用户输入quit 之前,该程序必须永远不会结束。

所以我的问题是,当用户输入 quit 而不是输入另一个单词时,我会输入什么 while(>> here <<) 来结束程序。单词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)");
        }
    }   
}

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

【问题讨论】:

  • 您说输入QUIT,但equals() 不会忽略大小写。此外,如果您希望循环运行直到有人键入退出,您可能希望在条件中使用!...
  • 使用do-while 循环并检查value 是否不等于quite...do {...} while (!guess.equalsIgnoreCase("quit"));

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


【解决方案1】:

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

【讨论】:

  • 我建议我们 quit.equalsIgnoreCase(guess) 所以处理不区分大小写。
  • @Gremash 谢谢,equalsIgnoreCase 确实会更好。
  • @Gremash 是的,我会回去修好那些小东西。我有根据我将如何使用程序来编写的习惯,在这种情况下,我会知道总是大写 QUIT。但我改变了它。谢谢。 :)
【解决方案2】:

您的循环 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)");
        }
}   
}

【讨论】:

    【解决方案3】:

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

      使用以下条件:

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

      【讨论】:

        【解决方案5】:

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

        【讨论】:

        【解决方案6】:

        我错了,使用“!=”会更好

        "quit.compareTo(guess)!=0" 在你的时间里

        【讨论】:

        • 这与使用equals相比没有任何好处。
        猜你喜欢
        • 2018-07-25
        • 2014-04-12
        • 1970-01-01
        • 1970-01-01
        • 2019-03-19
        • 2021-09-28
        • 1970-01-01
        • 2013-11-16
        • 2020-02-15
        相关资源
        最近更新 更多