简体   繁体   中英

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.

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.

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.

You can either prompt your user for input within your loop, or add a break statement to your "enter" block.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM