【问题标题】:is there somebody that can fix this if statement [duplicate]有没有人可以解决这个if语句[重复]
【发布时间】:2020-09-16 09:16:05
【问题描述】:
static void Main(string[] args)
{
    string name01;
    string name02;

    Console.WriteLine("Welcome to Choice RPG! <press enter to start your adventure>");
    Console.ReadKey();
    Console.WriteLine("Enter your Adventurers name! <press enter to continue>");
    name01 = Convert.ToString(Console.ReadLine());
    Console.WriteLine(name01 + "! come on, you can think of a better name than that can't you? <press enter to continue>");
    Console.ReadKey();
    Console.WriteLine("Enter your Adventurers name! <press enter to continue>");
    name02 = Convert.ToString(Console.ReadLine());

    if (name02 == name01)
    {
        Console.WriteLine("Wow, i guess you really like that name huh, fine.");
    }
    else
    {
        Console.WriteLine("Now thats more like it!");
    }
}

这是我的第一个一般编程项目。我正在制作一个 RPG 游戏,您可以在其中选择冒险,这是用户的游戏名称。

一切正常,直到 if 语句(之后 cmd 刚刚关闭)。如果有人有任何解决它的想法,请告诉我。

【问题讨论】:

  • 在 if 语句之后程序完成执行并且不等待任何额外的操作,因为它已经关闭。为了防止它关闭,强制它等待用户的按键输入。
  • 使用Console.ReadKey();
  • Console.ReadLine 返回一个string。你不必Convert.ToString它。

标签: c#


【解决方案1】:

代码在我看来没问题,请尝试添加:

Console.WriteLine("Press <ENTER> to exit");
Console.Readline();

最后看看你想显示的消息是否真的在应用程序退出和关闭之前显示。我的猜测是您在 Visual Studio 中运行,并打印了消息,但是执行窗口关闭得如此之快,以至于您看不到它。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-12-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-07-06
    • 2020-06-26
    • 2021-03-05
    相关资源
    最近更新 更多