【问题标题】:Trying to learn how to use IndexOf in VB.net尝试学习如何在 VB.net 中使用 IndexOf
【发布时间】: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

结束模块

【问题讨论】:

    标签: vb.net indexof


    【解决方案1】:

    你不能这样打电话给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
    

    【讨论】:

    • 该死,真不幸。感谢您的回复!
    • @Jason,您可以编写一个可以按照您的意愿工作的辅助方法
    • 辅助方法?我不太确定那是什么。
    • 哦!我现在明白了。非常感谢您的帮助和耐心!
    • @MrCoDeXeR,但我同意你的观点,array.indexof 在这里会更好用,所以我更新了我的代码,但保留了辅助方法,因为他不知道它是什么
    猜你喜欢
    • 2016-09-16
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-09-23
    相关资源
    最近更新 更多