【发布时间】:2014-11-01 01:35:32
【问题描述】:
我刚开始学习如何使用 VB.net,我正在尝试弄清楚如何使用 IndexOf 方法。我以为我可以用它来搜索字符串中的多个元素,但我收到一条错误消息,上面写着“参数匹配参数 'startIndex' 从 'String' 缩小到 'Integer'”,我不确定为什么。这是我正在使用的代码。
Module Module1
Sub Main()
Dim cont As Integer = 0
Do Until cont = 1
Dim PlayerChoice As String
Dim ComputerChoice As String
Dim ran As New Random
Dim num As Integer = ran.Next(1, 4)
Dim PlayerName As String
Dim check As Integer
Console.WriteLine("Welcome to Rock, Paper, Scissors!")
Console.WriteLine("Please enter your name.")
PlayerName = Console.ReadLine()
Console.Clear()
Console.WriteLine("Would you like to choose rock, paper, or scissors?")
PlayerChoice = Console.ReadLine()
Do Until check <> -1
check = PlayerChoice.IndexOf("rock", "paper", "scissors")
Console.Clear()
Loop
Select Case num
Case 1
ComputerChoice = "rock"
Case 2
ComputerChoice = "paper"
Case 3
ComputerChoice = "scissors"
End Select
Console.WriteLine(PlayerName & ": " & PlayerChoice)
System.Threading.Thread.Sleep(750)
Console.WriteLine("Computer: " & ComputerChoice)
If PlayerChoice = "rock" Then
If ComputerChoice = "rock" Then
Console.WriteLine("There was a tie!")
ElseIf ComputerChoice = "paper" Then
Console.WriteLine("The computer wins!")
ElseIf ComputerChoice = "scissors" Then
Console.WriteLine("You win, " & PlayerName & "!")
End If
ElseIf PlayerChoice = "paper" Then
If ComputerChoice = "rock" Then
Console.WriteLine("You win, " & PlayerName & "!")
ElseIf ComputerChoice = "paper" Then
Console.WriteLine("There was a tie!")
ElseIf ComputerChoice = "scissors" Then
Console.WriteLine("The computer wins!")
End If
ElseIf PlayerChoice = "scissors" Then
If ComputerChoice = "rock" Then
Console.WriteLine("The computer wins!")
ElseIf ComputerChoice = "paper" Then
Console.WriteLine("You win, " & PlayerName & "!")
ElseIf ComputerChoice = "scissors" Then
Console.WriteLine("There was a tie!")
End If
End If
Console.ReadLine()
Console.Clear()
Console.WriteLine("Would you like to play again?")
Console.WriteLine("Enter 0 to play again.")
Console.WriteLine("Enter 1 to quit.")
Console.ReadLine()
Console.Clear()
Loop
End Sub
结束模块
【问题讨论】: