我正在为C#的控制台应用程序中的冒险类文本游戏工作。如何检查用户是否正确输入命令 - C#
我需要一种方法来测试用户是否正确地输入命令,并且他们是否不再测试。
我现在尝试这一权利:
do
{
Response = Console.ReadLine();
switch (Response.ToLower())
{
case "hallway":
Location = Locations[2];
Console.WriteLine("You decide to get some fresh air, and step out of the dance room and into the hallway." + "\n" + "There's no one here.");
Console.ForegroundColor = ConsoleColor.Cyan;
Console.WriteLine("Command List: Look, Look at, Move, Check Status");
Console.ResetColor();
ResponseTester();
break;
case "dance room":
//[Other code here]
break;
default:
Console.WriteLine("I'm sorry, I don't understand that.");
break;
}
}
while (Response.ToLower() != "hallway" || Response.ToLower() != "dance room");
但是它不是很可靠的,因为当我尝试它与if/else语句或其他用途,这将只测试一次。有没有更好的方法来测试?
你可以有用户输入和语法检查位于无限循环中'而(真){...}'。否则就不清楚*“它只会测试一次”*有什么问题。 – Sinatr
@Sintar如果用户没有正确输入,它将不会重置循环,以便它们可以正确输入,这就是我的意思。就像我输入移动,然后不正确拼写“走廊”,它不会再尝试,只输出它不明白。 –