我写了一个拼写检查程序,将要求用户提供一个字,将它,我做了一个阵列比较单词列表。退出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循环。
你说'型QUIT',但'等于()'不忽略大小写。另外,如果你希望循环运行_until_某人类型退出,你可能会希望在条件中使用'!'而不是... – 2014-10-29 23:42:43
使用'do-while'循环并检查'value'是否等于'quite' ...'做{...}而(guess.equalsIgnoreCase( “退出”)!);' – MadProgrammer 2014-10-29 23:43:06