我正在VB中制作摇滚,纸张,剪刀游戏。我正在寻找一种方法来完成以下工作:
Select Case userInput
Case = "rock" And computerOutput = "scissors"
Console.WriteLine("You win! You picked " & userInput & ", and the computer picked " & computerOutput & "! Rock beats Scissors, hit enter to continue...")
Console.ReadLine()
userScore = userScore + 1
userInput和computerInput总是摇滚,纸张或剪刀。
但是当我尝试它时,我收到以下错误:
System.InvalidCastException: 'Conversion from string "rock" to type 'Boolean' is not valid.'
我对VB很新,所以不确定该怎么做。
答案 0 :(得分:1)
您可以按照此处写下SELECT CASE
声明:
Select [ Case ] testexpression
[ Case expressionlist
[ statements ] ]
[ Case Else
[ elsestatements ] ]
End Select
对于您的代码,您可以尝试这样:
Select Case userInput
Case "rock"
if computerOutput = "scissors" then
Console.WriteLine("You win! You picked " & userInput & ", and the computer picked " & computerOutput & "! Rock beats Scissors, hit enter to continue...")
Console.ReadLine()
userScore = userScore + 1
end if
答案 1 :(得分:1)
错误代码告诉你一切。您正在将字符串类型转换为布尔类型。您应该根据字符串的结果使用IF语句 do stuff ,而不是这样做。例如:
Select Case userInput
Case "rock"
if computerOutput = "scissors" then
Console.WriteLine("You win! You picked " & userInput & ", and the
computer picked " & computerOutput & "! Rock beats Scissors, hit enter
to continue...")
Console.ReadLine()
userScore = userScore + 1
end if
但是我没有看到什么会抛出这个错误。请发布您的所有代码。