编译器告诉我“不能将类型'char'隐式转换为'bool'
问题描述:
控制台告诉我在(28,22)它”不能隐式地将类型'char'转换为'bool'“,但在那个地方如果(ch2 ='c') 确切的地方在'('和'c'之间,那么这个地方在代码 中。在此先感谢您的帮助!编译器告诉我“不能将类型'char'隐式转换为'bool'
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')
,因为如果()需要一个布尔值,如果您使用==
operartor它会返回一个布尔值
哇,我觉得现在这么愚蠢。一定是错过了,因为太累了。感谢您的帮助! – IDOMATH 2014-09-19 03:52:24