编译器告诉我“不能隐式地将类型'char'转换为'bool'

时间:2014-09-19 03:48:42

标签: c#

控制台告诉我,在(28,22)它"不能隐式转换类型' char'到了' bool'"但是在那个地方,我使用的是一个char变量,我并没有尝试将它转换为bool。这是代码中的这个位置 否则如果(ch2 =' c') 确切地点在'('和' c'之间。提前感谢您的帮助!

using System;

public class EntranceChecking
{
    public static void Main()
    {

        char ch1, ch2;// Input character
        bool guess = false; // Flag that signals when the loop should terminate --
        //   that is, when 'c' followed by 's' has been input.
        Console.WriteLine("\nYou have before you a closed door.");
        Console.WriteLine("You must give the correct password to enter");

        Console.WriteLine("Enter a character : ");
        ch1 = Console.ReadKey().KeyChar;


        // Insert a loop that keeps reading and processing characters
        // until the user inputs the character 'c' followed by the
        // character 's' (if ever).

         while (!guess)
        {
            Console.WriteLine("Enter a character : ");
            ch2 = Console.ReadKey().KeyChar;
            if ((ch1 == 'c') && (ch2 == 's'))
                guess = true;
            else if (ch2 = 'c')
            {
                ch1 = 'c';
                break;
            }
            else
                ch1 = Console.ReadKey().KeyChar;
        }

        // Open the door.
        Console.Write("The door opens. Congratulations!");
    }

}

1 个答案:

答案 0 :(得分:2)

更改此

else if (ch2 = 'c')

代表

else if (ch2 == 'c')

因为if()需要一个布尔值,如果使用==操作符,它将返回一个布尔值