【问题标题】:having only the "Y" and "N" keys make the while loop function只有“Y”和“N”键使while循环功能
【发布时间】:2017-10-12 04:50:51
【问题描述】:

我如何做到这一点,所以这个循环只有在按下“N”键时才会重复。目前按“Y”会退出,按“N”会清除并继续游戏,但如果按其他键也会继续?

while (cont)
{
    theUI.InScreen();
    goodBye.ClosingMessage();
    Console.WriteLine("Do you want to exit Y/N?");
    string exitInput = Console.ReadLine();

    if (exitInput == "Y")
    {
        cont = false;
    }

    if (exitInput == "N")
    {
        cont = true;
        Console.Clear();
    }
}

【问题讨论】:

  • 请包含当前代码
  • 刚刚用当前代码更新了我的错误

标签: c# loops while-loop


【解决方案1】:

你需要像这样修改你的代码:

cont = true;
while (cont)
{
    theUI.InScreen();
    goodBye.ClosingMessage();
    Console.WriteLine("Do you want to exit Y/N?");
    string exitInput = Console.ReadLine();
    if (exitInput.ToLower() == "y")
    {
        cont = false;
    }
    else
    {
        Console.Clear();
    }
}

这样,如果您点击“y”或“Y”,游戏将退出。否则,它将继续循环。您无需再次将cont 设置为true,因为它不会改变。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2012-12-29
    • 2013-01-24
    • 1970-01-01
    • 2012-08-29
    • 2013-08-18
    • 1970-01-01
    • 2015-09-28
    • 1970-01-01
    相关资源
    最近更新 更多