我正在为 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
像这样:
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
我认为你试图以错误的方式解决它。
让我们一起了解一下逻辑:我们想检查数组,看看文本框中的名称是否在数组内:
我们从文本框中获取字符串:
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"。对布兰登·
并检查我们是否可以找到它:
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 中:
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")
以下是您修复当前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