class Program
{
static void Main(string[] args)
{
if (Console.ReadLine() == "y")
Console.WriteLine("You typed y");
Console.ReadLine();
}
}
你也可以做
if (Console.ReadLine().ToLower() == "y")
Console.WriteLine("You typed y");
如果你正在做char c =(char)Console.Read();然後無論您爲Console.Read輸入什麼內容,您都必須繼續輸入Console.Readline()旁邊的Console.Readline()語句才能正常工作。
或者您可以使用 如果(到Console.ReadLine()。ToLower將()。修剪()== 「Y」)
但對於這個工作,你鍵入的東西Console.Read()
喜歡輸入值N,然後你必須輸入y以便下一個Console.ReadLine進行評估。
像這樣。
n y
class Program
{
static void Main(string[] args)
{
char c = (char)Console.Read();
if (Console.ReadLine().ToLower().Trim() == "y")
Console.WriteLine("You typed y");
Console.ReadLine();
}
//input: n y
//output: You typed y
}
也有一些是勉強接近沖洗是Console.Clear()的方法,但,這也清除顯示以及緩衝器。