[英]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.