2013-10-13 53 views
0

我只是一名10年級的編程學生。錯誤是:Conversion from string "alan" to type 'Boolean' is not valid。我永遠無法繞過它。錯誤發生在(If firstname = "alan" Or "Alan" Then)。下面是代碼:如何解決錯誤:從字符串「alan」到類型「Boolean」的轉換無效

Module Module1 

Sub Main() 
    Dim firstname As String 
    Console.WriteLine("Hello and welcome to Alan's Computer: Press enter to continue") 
    Console.ReadLine() 
    Console.WriteLine("Please enter your first name") 
    firstname = Console.ReadLine() 
    If firstname = "alan" Or "Alan" Then 
     Console.WriteLine("Welcome") 

    Else 
     Console.WriteLine("You may not enter {0}", firstname) 
    End If 

End Sub 

End Module 

回答

1

正確的語法是

If firstname = "alan" OrElse firstname = "Alan" Then 
    Console.WriteLine("Welcome") 
Else 
    Console.WriteLine("You may not enter {0}", firstname) 
End If 

您需要重複表達firstname = <value>的兩個病例,但你也需要用更合適OrElse

更換或者

請參閱Or vs OrElse

+0

我嘗試將代碼更改爲此,但無效。同樣的錯誤出現 –

+0

你能告訴我你的錯誤嗎? – Steve

+0

從字符串「alan」到類型「Boolean」的轉換無效。 –

相關問題