编译器告诉我“;无法隐式转换类型';char';到';bool';
本文关键字:类型 char 转换 bool 告诉我 编译器 | 更新日期: 2023-09-27 18:22:43
控制台告诉我,在(28,22),它"无法将类型‘char’隐式转换为‘bool’",但在那个位置,我使用的是一个char变量,而不是试图将其转换为bool。代码中的这个位置else if(ch2='c')确切的位置在"("answers"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!");
}
}
更改此
else if (ch2 = 'c')
对于
else if (ch2 == 'c')
因为if()需要一个布尔值,所以如果使用==
运算符,它将返回一个布尔