试图学习如何在VB.net中使用IndexOf

时间:2014-11-01 01:35:32

标签: vb.net indexof

我刚开始学习如何使用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

结束模块

1 个答案:

答案 0 :(得分:2)

你不能那样打电话给indexof

如果您read the documentation,您会发现必须一次只搜索1个字符串。在你的情况下,你必须做3次或直到你找到匹配

你可以实现像这样的辅助方法

Imports System.Runtime.CompilerServices

Module Module1

    Sub Main()
        Dim test As String = "paper"
        Console.WriteLine(test.IndexOf("rock", "paper", "scissors"))

        test = "rock"
        Console.WriteLine(test.IndexOf("rock", "paper", "scissors"))

        test = "scissors"
        Console.WriteLine(test.IndexOf("rock", "paper", "scissors"))

        test = "foobar"
        Console.WriteLine(test.IndexOf("rock", "paper", "scissors"))

        Console.ReadKey()
    End Sub

End Module

Public Module helper

    <Extension()>
    Public Function IndexOf(ByVal value As String, ParamArray values() As String) As Integer
        Return Array.IndexOf(values, value)
    End Function
End Module