2014-09-19 67 views
-2

控制檯告訴我在(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!"); 
    } 

} 

回答

2

改變這種

else if (ch2 = 'c') 

else if (ch2 == 'c') 

,因爲如果()需要一個布爾值,如果您使用== operartor它會返回一個布爾值

+0

哇,我覺得現在這麼愚蠢。一定是錯過了,因爲太累了。感謝您的幫助! – IDOMATH 2014-09-19 03:52:24

相關問題