vba - Visual Basic 学校项目
问题描述
Visual Basic 不是我的主要语言,我正试图帮助我的儿子完成一个学校项目,但惨遭失败。该程序假设读取一个文件,创建一个 ID 并将其保存到磁盘。一切正常,除了最后两个程序,确定最短游泳时间,(它这样做)但我试图将用户的名字分配给最快的游泳者,并在最后一个程序中显示获胜者和最短时间。
它正在读取的文件只是一个文本文件,格式为名字、姓氏、年龄和性别。我最快的游泳者总是将空引用传递给该方法。任何帮助将不胜感激。
这是整个程序:
Public Class Form1
Private Sub StartBtn_Click(sender As Object, e As EventArgs) Handles StartBtn.Click
Dim firstName(5) As String
Dim surname(5) As String
Dim age(5) As String
Dim gender(5) As String
Dim swimID(5) As String
Dim time(5) As Single
Dim fastestSwimmer As String
Dim minTime As Single
'Call Procedures
Call GetDetails(firstName, surname, age, gender)
Call CalcSwimID(swimID, firstName, surname, age, gender)
Call RaceTimes(time, firstName, surname)
Call CalcWinner(time, firstName, fastestSwimmer, minTime)
'Call WinnersCircle(fastestSwimmer, minTime)
End Sub
'Get details from reading a file
Private Shared Sub GetDetails(ByVal firstName As String(), ByVal surname As String(), ByVal age As String(), ByVal gender As String())
Dim filename As String
filename = "C:/Users/rk/source/repos/CleanFolder/SwimersChampionShip/details.txt"
FileOpen(1, filename, OpenMode.Input)
For counter = 1 To 5
Input(1, firstName(counter))
Input(1, surname(counter))
Input(1, age(counter))
Input(1, gender(counter))
Next
FileClose(1)
End Sub
'Create a Swimmers ID and save it back to disk
Private Shared Sub CalcSwimID(ByRef swimID As String(), ByRef firstName As String(), ByRef surname As String(), ByRef Age As String(), ByRef gender As String())
Dim genderAscii(5) As Integer
Dim firstCharacter(5) As String
For counter = 1 To 5
firstCharacter(counter) = firstName(counter).Substring(0, 1)
genderAscii(counter) = Asc(gender(counter))
swimID(counter) = genderAscii(counter) & "-" & firstCharacter(counter) & surname(counter) & "-" & Age(counter)
Next
Dim savedFile As String
savedFile = ("C:/Users/rk/source/repos/CleanFolder/SwimersChampionShip/competitorsID.txt")
FileOpen(1, savedFile, OpenMode.Output)
For counter = 1 To 5
PrintLine(1, swimID(counter))
Next
FileClose(1)
End Sub
' Gets the race times by input box
Private Shared Sub RaceTimes(ByRef time As Single(), ByRef firstName As String(), ByRef surname As String())
For counter = 1 To 5
time(counter) = InputBox(" Please enter the swim times for " & firstName(counter) & " " & surname(counter))
Next
'MsgBox(time)
End Sub
'This will calculate the winner
Private Sub CalcWinner(ByRef time As Single(), ByRef firstName As String(), ByVal fastestSwimmer As String, ByRef minTime As Single)
'MsgBox(time(1))
minTime = time(1) And fastestSwimmer = firstName(1)
For counter = 2 To 5
If time(counter) < minTime Then
minTime = time(counter) And fastestSwimmer = firstName(counter)
End If
Next
'MsgBox(minTime)
End Sub
' Displays winner of the race to the winners circle, Gold medal will be sent by mail!
Private Sub WinnersCircle(ByRef fastestSwimmer As String, ByRef minTime As Single)
ListBox1.Items.Add("Congradulations " & fastestSwimmer & " you are the winner your time was " & minTime & " seconds. A new world record!")
End Sub
Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
Me.Close()
End Sub
Private Sub ListBox1_SelectedIndexChanged(sender As Object, e As EventArgs) Handles ListBox1.SelectedIndexChanged
End Sub
End Class
解决方案
在 CalcWinner 方法中,您以错误的方式使用 AND:
Private Sub CalcWinner(ByRef time As Single(), ByRef firstName As String(), ByVal
fastestSwimmer As String, ByRef minTime As Single)
'MsgBox(time(1))
minTime = time(1)
fastestSwimmer = firstName(1)
For counter = 2 To 5
If time(counter) < minTime Then
minTime = time(counter)
fastestSwimmer = firstName(counter)
End If
Next
'MsgBox(minTime)
End Sub
当你写
minTime = time(1) And fastestSwimmer = firstName(1)
VB 将添加第一次(时间(1))和fastestSwimmer = firstName(1),这是0,因为fastestSwimmer 不等于firstName(1),因此计算结果为0