简体   繁体   English

使用do-while循环检查Java中用户的输入

[英]Using a do-while loop to check a User's input in Java

I am trying to create a short text-based adventure game using java. 我正在尝试使用Java创建一个简短的基于文本的冒险游戏。

I am a beginning programmer, so my methods may not be the most efficient, but I am using only what I have learned from my school courses so far. 我是一名新手程序员,所以我的方法可能不是最有效的,但是我仅使用到目前为止从学校课程中学到的知识。

My problem occurs after asking the user for their input. 我的问题出现在询问用户输入之后。

import java.util.Scanner;
public class House
{
    public static void main(String[]args)
    {

        System.out.println("You have just moved into your new house.");
        System.out.println("What do you do next?");
        Scanner scan = new Scanner(System.in);
        String input = scan.next();

        do
        {
            if(input.equals("enter") || input.equals("Enter"))
            {
                System.out.println("You have entered the house.");
            }

            else if(input.equals("leave") || input.equals("Leave"))
            {
                System.out.println("You left and never came back.");
                System.out.println("The End");
                System.exit(0);
            }

            else
            {
                System.out.println("I don't know how to "+input);
                System.out.println("Try again");
                System.out.println("What do you do next?");
                input = scan.nextLine();
            }
        }
        while(!input.equals("enter") || !input.equals("Enter") || !input.equals("leave") || !input.equals("Leave"));

    }
}

The program works as expected if the user enters "leave", "Leave", or an unknown command. 如果用户输入“ leave”,“ Leave”或未知命令,程序将按预期工作。 The problem occurs when the user enters "enter" or "Enter" 当用户输入“ enter”或“ Enter”时,会发生此问题

The condition in my do-while loop should make the program exit the loop whenever their input is equal to "enter" or "Enter", but the program gets stuck in an endless loop instead. 只要输入等于“ enter”或“ Enter”,do-while循环中的条件就应使程序退出循环,但程序将陷入无限循环。

The program will simply repeat, "You have entered the house." 该程序将简单地重复“您已进入房屋”。 over and over. 一遍又一遍。

Why is this problem occuring? 为什么会出现此问题? I have no clue what I am doing wrong. 我不知道我在做什么错。

Thanks! 谢谢!

Change it to 更改为

...
String input;
do
{
    input = scan.next();
    if(input.equals("enter") || input.equals("Enter"))
    {
        System.out.println("You have entered the house.");
    }
...

Since you are not changing the value of the input variable inside your "enter" block, when this block is hit it will continue to loop and end up back there indefinitely. 由于您没有在“ enter”块内更改输入变量的值,因此,当该块被点击时,它将继续循环并无限期地返回到那里。

You can either prompt your user for input within your loop, or add a break statement to your "enter" block. 您可以循环中提示用户输入,也可以在“输入”块中添加一个break语句。

I will opt for the latter in this case, as it will not change the behavior of your code as drastically as moving the input prompt inside your loop. 在这种情况下,我将选择后者,因为它不会像在循环内移动输入提示那样剧烈地改变代码的行为。

if(input.equals("enter") || input.equals("Enter"))
{
    System.out.println("You have entered the house.");
    //break;
    // or use system.exit as in your other block
    System.exit(0);
}

Just FYI, instead of: 仅供参考,而不是:

if(input.equals("enter") || input.equals("Enter"))

use 采用

input.equalsIgnoreCase("enter")

It will eliminate your "or" clauses and shorten your code a little bit 它将消除您的“或”子句,并缩短您的代码

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 用户输入时执行Do-While循环-检查输入是否为空或int(java) - Do-While loop on user input - check if input is not empty or int (java) 如何使用do-while循环再次检查用户输入? - How to use do-while loop to check user input again? Java - 通过用户输入退出 do-while 循环 - Java - get out of the do-while loop with an input of user 使用do-while循环将用户输入添加到ArrayList - Adding user input to ArrayList using do-while loop Java如何使用用户的布尔输入来结束do-while循环? - Java how to end a do-while loop using boolean input from user? Do-while循环不等待用户输入? - Do-while loop not waiting for user input? Java:尝试通过do-while限制用户输入时,在while循环中进行无限循环 - Java: Infinite looping within while loop when trying to limit user input with do-while Do-While 循环在输入用户输入时显示很多错误(Java -OSGi Equinox in Eclipse EE - Do-While loop is showing a lot of error while entering user input (Java -OSGi Equinox in Eclipse EE 在 do-while 循环中尝试/捕获以检查用户输入(数组) - 错误的 boolean 初始化和 position - Try / catch in a do-while loop to check user input (array) - wrong boolean initialization and position Java输入缓冲区和do-while循环行为(为什么它会检查第一个字符3次?) - Java input buffer and do-while loop behavior (why does it check first character 3 times?)
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM