无法转换系统.Consolekeyinfo为字符串

本文关键字:字符串 Consolekeyinfo 系统 转换 | 更新日期: 2023-09-27 18:09:12

我想在这里实现的是,当你按下"1",它会检查它对"code_1",然后如果它匹配它会说"key1正确",然后检查其他代码。但是编译器说

不能转换系统。Consolekeyinfo到string

所以我想知道我如何解决这个问题。下面是我使用的代码:

    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
            {
            }
        }
    }
}

无法转换系统.Consolekeyinfo为字符串

当前得到的错误是因为您忘记了:

=和==不是一回事。第一个是赋值操作,第二个是比较操作。

您不能将string赋值给ConsoleKeyInfo,反之亦然,并且绝对不能在if语句中。即使你已经解决了这个问题,你仍然不能比较stringConsoleKeyInfo。你可以获取它的KeyChar属性,并将其与char进行比较:

if (keyInfo.KeyChar == myString[0])

是有效的(因为string可以被索引以获得它的char s)。在您的情况下,您可以使用一个字符,使它更简单:

if (keyInfo.KeyChar == '1')

ToString()可以工作。假设我们有ConsoleKeyInfo j.

它看起来像string k = j.KeyChar.ToString();

这将完全符合您的要求。

所以代码看起来像:

ConsoleKeyInfo key1 = Console.ReadKey();
if (code_1 == key1.KeyChar.ToString())
{
//Other stuff here as follows.  
}

你甚至可以这样做

if (key1.KeyChar.ToString() == "1")
{
//Other stuff here as follows
}

使用Console.Read();相反,它返回一个可以被类型转换为char的int类型。另外,您可以使用包含完整代码的字符串,而不是包含一个字符的4个字符串,并将其用作数组,参见下面的示例

    static void Main(string[] args)
    {
        string pw = "123";
        Console.WriteLine("Enter the first digit of the password");
        char toTest = (char) Console.Read();
        Console.Read();
        Console.Read();
        if (toTest == pw[0])
        {
            Console.WriteLine("Enter the second digit of the password");
            toTest = (char)Console.Read();
            Console.Read();
            Console.Read();
            if (toTest == pw[1])
            {
                Console.WriteLine("Enter the third digit of the password");
                toTest = (char)Console.Read();
                Console.Read();
                Console.Read();
            }
        }
    }

额外的Console.Read();命令用来捕获按回车键时输入的不可见字符。