Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/329.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如果用户键入Quit,则退出While循环_Java_Loops_If Statement_For Loop_While Loop - Fatal编程技术网

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

Java 如果用户键入Quit,则退出While循环,java,loops,if-statement,for-loop,while-loop,Java,Loops,If Statement,For Loop,While Loop,我正在编写一个拼写检查器程序,它将要求用户输入一个单词,并将其与我制作的数组中的单词列表进行比较 在用户键入退出之前,此程序必须永不结束 所以我的问题是,当(>>此处您的条件始终为真时,我将如何放置。如果您想在输入新猜测时对用户键入“quit”作出反应,您可以使用无限循环和中断,如下所示: while (true) { System.out.println("Please every a word or QUIT to exit"); String guess = input.n

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

在用户键入
退出之前,此程序必须永不结束


所以我的问题是,当(>>此处您的条件始终为真时,我将如何放置
。如果您想在输入新猜测时对用户键入
“quit”
作出反应,您可以使用无限循环和
中断
,如下所示:

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++) {
        ...
    }
}
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"));
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();
}
while(true){
System.out.println(“请每说一个字或退出退出”);
字符串guess=input.nextLine();
if(quit.equalsIgnoreCase(猜测)){
打破
}
for(int i=0;i
我错了,使用“!=”会更好


在你的while中“quit.compareTo(guess)!=0”使用Do-while循环而不是简单的while循环

代码如下所示:

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++) {
        ...
    }
}
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"));
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();
}
String quit=“quit”;
//这仅声明为“退出”,以使循环运行以进行测试
做{
System.out.println(“请每说一个字或退出退出”);
字符串guess=input.nextLine();
for(int i=0;i
您的while循环将继续,直到您将字符串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(quit.equals(“quit”)){
System.out.println(“请每说一个字或退出退出”);
字符串guess=input.nextLine();
for(int i=0;i
如果您必须在输入后立即退出,并且必须使用
while
条件来触发它,那么您的输入必须位于循环的末尾,以便在输入后立即检查该条件。您可以这样做:

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++) {
        ...
    }
}
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"));
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();
}
System.out.println(“请输入一个单词或退出以退出”);
字符串guess=input.nextLine();
而(!guess.equalsIgnoreCase(“退出”)){
for(int i=0;i
使用以下条件:

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

你说键入
QUIT
,但是
equals()
不会忽略大小写。另外,如果你想让循环一直运行到有人键入QUIT,你可能需要在条件中使用
…使用
do while
循环,并检查
值是否不等于
相当
do{…}(!guess.equalsIgnoreCase(“quit”);
这对使用
equals
没有好处。我建议我们退出.equalsIgnoreCase(guess)所以处理是不区分大小写的。@Gremash谢谢,
equalsIgnoreCase
确实会更好。@Gremash是的,我会回去解决这些小问题。我有一个习惯,就是根据我将如何使用程序来写,在这种情况下,我会知道总是大写“退出”。但我改变了它。谢谢。)@AndyFedoroff请停止为强调添加代码格式,请参阅并停止将所有内容置于引号中