我刚刚开始学习一些实际的OOP。这是我的第一周编程,所以我想我可以尝试在CMD提示下创建一个基本游戏。我正在尝试让玩家在两件事之间进行选择。截至目前,该程序仅询问“您准备好开始冒险了吗”。一切都按预期进行,直到进入选择部分。输入答案后,CMD提示符将不显示任何内容就跳至下一行并退出。关于我做错了什么的任何建议,将不胜感激。
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace InteractiveGame
{
class Program
{
static void Main(string[] args)
{
string CharName = "John Doe";
string InvalidInput = "This is an invalid answer, please choose a
correct option";
string GameOver = "You Died, Game Over";
Console.WriteLine("Welcome to Dragon Land!");
Console.WriteLine("What is your name young traveler?");
CharName = Console.ReadLine();
Console.WriteLine("Are you ready to begin your adventure " +
CharName + "?");
Console.WriteLine("Y/N?");
Console.ReadLine();
var answer = Console.Read();
if (answer == 'Y' || answer =='y')
{
Console.WriteLine("You begin your journey!");
}
if (answer == 'N' || answer == 'n')
{
Console.WriteLine(GameOver);
}
else
{
Console.WriteLine(InvalidInput);
}
Console.ReadLine();
}
}
}
答案 0 :(得分:0)
您的问题在这里:
Console.ReadLine();
var answer = Console.Read();
您正在放弃用户给您的第一个答案。您应该这样做:
var answer = Console.ReadLine();
此外,如果用户输入“ Y”,“ y”,“ N”或“ n”以外的任何内容,您的应用程序将表现异常。如果用户输入“ D”,您希望程序做什么?您可能希望用户再试一次,不是吗?您如何使用当前设置实现这一目标?似乎很难...
在构建这些类型的cmd应用程序时,您应该熟悉一些工具:
if-elseif-else
语句do-while
循环(非常方便)