2014-10-29 81 views
0

我写了一个拼写检查程序,将要求用户提供一个字,将它,我做了一个阵列比较单词列表。退出While循环如果用户键入QUIT

这个程序需要被无休止,直到用户类型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循环

+0

你说'型QUIT',但'等于()'不忽略大小写。另外,如果你希望循环运行_until_某人类型退出,你可能会希望在条件中使用'!'而不是... – 2014-10-29 23:42:43

+1

使用'do-while'循环并检查'value'是否等于'quite' ...'做{...}而(guess.equalsIgnoreCase( “退出”)!);' – MadProgrammer 2014-10-29 23:43:06

回答

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++) { 
     ... 
    } 
} 
+0

我建议我们quit.equalsIgnoreCase(猜测),所以处理不区分大小写。 – Gremash 2014-10-29 23:48:07

+0

@Gremash谢谢,'equalsIgnoreCase'确实会更好。 – dasblinkenlight 2014-10-29 23:53:51

+0

@Gremash是的,我会回去修理那些小东西。我有一个基于我将如何使用该程序的写作习惯,在这种情况下,我会知道总是将QUIT字母大写。但我改变了它。谢谢。 :) – 2014-10-30 00:02:53

-1

我错了,它会工作更好” =在同时

+0

这对使用'equals'没有任何好处。 – khelwood 2014-10-29 23:47:07

0

使用do-while循环,而不是简单的While循环,“

”quit.compareTo(猜)!= 0“。

的代码看起来是这样的:

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

@AndyFedoroff请停止添加代码格式化为重点,见http://meta.stackexchange.com/q/135112/186983 别再把一切都放到引号 – Matsemann 2016-10-15 08:22:47

0

你的循环,同时会继续下去,直到你改变你的字符串跳槽不是“退出”以外的东西。这里有一个例子:

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

使用以下条件:

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