[英]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.