繁体   English   中英

出现提示时 C# 关闭控制台应用程序?

[英]C# close console application when prompted?

当用户输入一个字母时,C# 中是否有一种方法可以关闭控制台应用程序 - 而不使用命令 'Press any key to continue...' 。

即,如果我写下信息“你想退出(是/否)吗?” 用户输入字母 y。

namespace Example
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Do you really want to quit the Game? (y, n): ");
            char YN = Console.ReadLine()[0];


            if (YN == 'y' ||
                YN == 'Y')

            {

            }
            else if (YN == 'n' ||
                 YN == 'N')
            {
                Console.WriteLine("Continue Game...");
            }
        }
    }
}

在Y条件内使用Environment.Exit(0)

    class Program
    {
        static void Main(string[] args)
        {
            Console.Write("Do you really want to quit the Game? (y, n): ");
            char YN = Console.ReadLine()[0];


            if (YN == 'y' ||
                YN == 'Y')

            {
                Environment.Exit(0);
            }
            else if (YN == 'n' ||
                 YN == 'N')
            {
                Console.WriteLine("Continue Game...");
            }
        }
    }
}

你可以抓到钥匙

  char c = Console.     ReadKey().KeyChar ;

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM