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

时间:2022-10-11 21:23:23

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. 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.

我的do-while循环中的条件应该使程序在输入等于“enter”或“Enter”时退出循环,但程序会陷入无限循环。

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!

谢谢!

3 个解决方案

#1


3  

Change it to

将其更改为

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

#2


0  

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);
}

#3


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

它会消除你的“或”条款并缩短你的代码

#1


3  

Change it to

将其更改为

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

#2


0  

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);
}

#3


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

它会消除你的“或”条款并缩短你的代码