在.txt文件VB.net中搜索数组

时间:2015-04-22 13:31:59

标签: arrays vb.net

我正在为vb做一个学校项目,并且被要求编写一个程序,创建一个单维字符串数组,其中包含最后6人赢得高尔夫锦标赛的名字。用户必须在文本框中输入名称才能搜索数组。如果名称确实出现,则消息框应显示玩家姓名和获胜年份 如果没有出现,也会出现一条消息,说明这一点。

我已经写出了代码,但遇到了错误{'i'未被声明。由于它的保护级别可能无法访问}

我已经研究过这个错误并尝试过多种方法,但代码仍然不能正常工作。我很感谢任何帮助,谢谢你

       Imports System.IO
         Public Class Form1
              Dim Player() As String = {"Tim Wood", "Vince Singer","Donal Clark", "Patrick Hartnett", "Peter Nicholson", "Chris Montgomery"}

              Dim WinningYear() As Integer = {2002, 2003, 2004, 2005, 2006, 2007}



Private Sub btnSearch_Click(sender As Object, e As EventArgs)
    'Searches array for player
    Dim strPlayer As String = txtPlayer.Text
    Dim intYearWon As Integer

    For i As Integer = 0 To 6

        If i = 6 Then

            MessageBox.Show(strPlayer & " has not won the tournament in the last 6 years")
    End If
    Next

    intYearWon = WinningYear(i)
    If strPlayer = Player(i) Then
        MessageBox.Show(strPlayer & " won in " & intYearWon)

    End If

End Sub


   End Class

3 个答案:

答案 0 :(得分:2)

我认为你试图以错误的方式解决它。

让我们把逻辑放在一起: 我们想检查数组,看看文本框中的名字是否在数组中:

我们从文本框中获取字符串:

Dim strPlayer As String = txtPlayer.Text

我们开始循环:(我们从0到5运行,这是6个数据点(0,1,2,3,4,5))

For i as Integer = 0 To 5

另一种方法是更改​​Player.length - 1中的“5”。对布兰登B提起诉讼

并检查我们是否能找到它:

if Player(i) = strPlayer then

如果我们这样做,我们会显示消息框,然后返回:

    intYearWon = WinningYear(i)
    MessageBox.Show(strPlayer & " won in " & intYearWon)   
    Return

关闭你的if:

end if

转到下一个循环:

next

如果我们到达此代码,这意味着我们找不到名称..所以现在我们显示其他文本框:

MessageBox.Show(strPlayer & " has not won the tournament in the last 6 years")

我们去..你需要的所有代码..

现在一起去:

Dim strPlayer As String = txtPlayer.Text
For i as Integer = 0 To 5
    if Player(i) = strPlayer then
        intYearWon = WinningYear(i)
        MessageBox.Show(strPlayer & " won in " & intYearWon)   
        Return
    end if
next
    MessageBox.Show(strPlayer & " has not won the tournament in the last 6 years")

答案 1 :(得分:2)

这样的事情:

Option Strict On
Option Explicit On
Option Infer Off
Public Class Form1
    Private Player() As String = {"Tim Wood", "Vince Singer", "Donal Clark", "Patrick Hartnett", "Peter Nicholson", "Chris Montgomery"}
    Private WinningYear() As Integer = {2002, 2003, 2004, 2005, 2006, 2007}
    Private Sub btnSearch_Click(sender As Object, e As EventArgs) Handles btnSearch.Click
        'Searches array for player
        Dim strPlayer As String = txtPlayer.Text
        Dim found As Boolean = False
        For i As Integer = 0 To Player.Length - 1
            If strPlayer.ToLower = Player(i).ToLower Then
                found = True
                MessageBox.Show(strPlayer & " won in " & WinningYear(i))
            End If
        Next
        If Not found Then MessageBox.Show(strPlayer & " has not won the tournament in the last 6 years")
    End Sub
End Class

答案 2 :(得分:-1)

以下是您如何修复当前的btnSearch_Click()方法:

Private Sub btnSearch_Click(sender As Object, e As EventArgs)
    'Searches array for player
    Dim strPlayer As String = txtPlayer.Text
    Dim intYearWon As Integer
    Dim success as boolean = False
    For i As Integer = 0 To Players.count - 1

        If strPlayer.equals(Player(i)) Then
            intYearWon = WinningYear(i)
            success = True
            MessageBox.Show(strPlayer & " won in " & intYearWon)  
        End If

    Next

    If Not success Then MessageBox.Show(strPlayer & " has not won the tournament in the last 6 years")

End Sub

但是,我会考虑使用Dictionary(Of Int32, String)多年和名字。请注意,我选择年份作为键,名称作为值。这是因为每年只有一个人可以获胜,但每个人都可以赢得多年。以下是您在代码中使用Dictionary(Of Int32, String)的方法:

Public Class Form1
    Dim playerDict As New Dictionary(Of Int32, String) From {{2002, "Tim Wood"}, {2003, "Vince Singer"}, {2004, "Donal Clark"}, {2005, "Patrick Hartnett"}, {2006, "Peter Nicholson"}, {2007, "Chris Montgomery"}}

    Private Sub btnSearch_Click(sender As Object, e As EventArgs)
        Dim strPlayer As String = txtPlayer.Text
        Dim years As IEnumerable(Of String) = From kvp In playerDict Where kvp.Value = strPlayer Select kvp.Key.ToString

        If years.Count <> 0 Then
            MessageBox.Show(strPlayer & " has won in the folowing years: " & String.Join(", ", years))
        Else
            MessageBox.Show(strPlayer & " has not won the tournament in the last " & playerDict.Keys.Count & " years")
        End If
    End Sub
End Class