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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/34364176/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-11-02 22:48:55  来源:igfitidea点击:

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

javainputjava.util.scannerdo-while

提问by Zampanò

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"

如果用户输入“离开”、“离开”或未知命令,程序会按预期工作。用户输入“回车”或“回车”时出现问题

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!

谢谢!

回答by Dacaspex

Change it to

将其更改为

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

回答by Ookie Ebron

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

它将消除您的“或”子句并稍微缩短您的代码

回答by OscuroAA

Since you are not changing the value of the inputvariable inside your "enter" block, when this block is hit it will continue to loop and end up back there indefinitely.

由于您没有更改“输入”块内输入变量的值,因此当该块被击中时,它将继续循环并无限期地回到那里。

You can either prompt your user for input withinyour loop, or add a breakstatement 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);
}