我試圖在這裏實現的是,當您按下「1」時,它會將其與「code_1」進行覈對,然後如果匹配,則會顯示「key1 correct」,然後檢查其他代碼。但是編譯器說無法將system.consolekeyinfo轉換爲字符串
不能system.consolekeyinfo轉換爲字符串
所以我不知道我該怎麼解決這個問題。下面是我使用的代碼:
static void Main(string[] args)
{
string first_time = null;
string paktc = "Press any key to continue . . .\r\n";
string code_1 = "1";
string code_2 = "2";
string code_3 = "3";
string code_4 = "4";
if (first_time == null)
{
Console.WriteLine("\r\nYour code is 1234\r\n");
Console.WriteLine(paktc);
Console.ReadKey();
Console.WriteLine("Insert Code Now\r\n");
ConsoleKeyInfo key1 = Console.ReadKey();
if (code_1 = key1)
{
ConsoleKeyInfo key2 = Console.ReadKey();
if (code_2 = key2)
{
ConsoleKeyInfo key3 = Console.ReadKey();
if (code_3 = key3)
{
Console.WriteLine("Key3 Correct\r\n");
ConsoleKeyInfo key4 = Console.ReadKey();
if (code_4 = key4)
{
Console.WriteLine("Key4 Correct\r\n");
Console.ReadKey();
Console.WriteLine(paktc);
}
else
{
}
}
else
{
}
}
else
{
}
}
else
{
}
}
}
}
添加否則效率不高。你以前是否用Switch語句編寫代碼? – Muks