我的程序有问题,该程序应该询问用户的驱动程序时间,用户将输入驱动程序时间,程序应该从最快到最慢排序然后显示在订购。 但是当我尝试运行程序时,驱动程序时间已经排序,但驱动程序名称尚未排序。
示例:
用户必须输入的内容。
Drivers name drivers time
Sebastian Williams 10
Tom Hamilton 6
Danny Ricardo 2
Walter Borras 7
Fernando Sonal 1
Jenson Smith 9
程序输出
1 Sebastian Williams
2 Tom Hamilton
6 Danny Ricardo
7 Walter Borras
9 Fernando Sonal
10 Jenson Smith
正如您所看到的,时间已经排序,但名称尚未按时间排序。我怎么能解决这个问题?
这是我的代码。
Public Class Form1
Public Sub cmdStart_Click(sender As Object, e As EventArgs) Handles cmdStart.Click
Dim name
Dim racetime
Dim team
input(Name, raceTime, team)
End Sub
Public Structure raceCar
Public Name() As String
Public raceTime() As Double
Public team() As String
End Structure
Private Sub input(ByRef name(), ByRef raceTime(), ByRef team())
Dim raceCars As raceCar
ReDim raceCars.Name(5)
ReDim raceCars.raceTime(5)
ReDim raceCars.team(5)
Dim filenameInput As String = "G:\grandPrixVB\Input\Input.csv"
Dim textfileInput As New System.IO.StreamReader(filenameInput)
Dim filenameOutput As String = "G:\grandPrixVB\Input\Input2.txt"
Dim textFileOutput As New System.IO.StreamWriter(filenameOutput)
For counter = 0 To 5
raceCars.Name(counter) = textfileInput.ReadLine
raceCars.team(counter) = textfileInput.ReadLine
textfileInput.ReadLine()
textfileInput.ReadLine()
raceCars.raceTime(counter) = InputBox("Enter the race time for " & raceCars.Name(counter) & ".")
ListBoxUnsorted.Items.Add(raceCars.Name(counter) & " is part of " & raceCars.team(counter) & " got a time of " & raceCars.raceTime(counter) & " seconds.")
textFileOutput.WriteLine(raceCars.Name(counter) & "," & raceCars.raceTime(counter))
Next
textfileInput.Close()
textfileInput.Dispose()
textFileOutput.Close()
textFileOutput.Dispose()
Dim i As Integer
ListBoxSorted.Items.Add("Unordered Array")
For i = LBound(raceCars.raceTime) To UBound(raceCars.raceTime)
ListBoxSorted.Items.Add(raceCars.raceTime(i))
Next
sortArray(raceCars.raceTime)
ListBoxSorted.Items.Add("")
ListBoxSorted.Items.Add("Ordered Array")
For i = LBound(raceCars.raceTime) To UBound(raceCars.raceTime)
ListBoxSorted.Items.Add(raceCars.raceTime(i) & raceCars.Name(i))
Next
End Sub
Private Sub sortArray(ByRef array() As Double)
Dim i As Double
Dim j As Double
Dim minimum As Double
Dim swapValue As Double
Dim upperBound As Double
Dim lowerBound As Double
lowerBound = LBound(array)
upperBound = UBound(array)
For i = lowerBound To upperBound
minimum = i
For j = i + 1 To upperBound
If array(j) < array(minimum) Then
minimum = j
End If
Next j
If minimum <> i Then
swapValue = array(minimum)
array(minimum) = array(i)
array(i) = swapValue
End If
Next i
End Sub
End Class
提前致谢
答案 0 :(得分:1)
您只是对raceTimes数组进行排序。你需要对你的raceCar变量中的其他数组做同样的事情,但幸运的是你可以背负现有的逻辑:
首先,您需要将其他数组传递给sortArray方法并创建&#34;交换变量&#34;对于每个人:
Private Sub sortArray(ByRef array() As Double, ByRef names() As String, ByRef teams() as String)
Dim swapName As String
Dim swapTeam As String
然后当您交换时间值时,您还可以交换姓名和团队:
If minimum <> i Then
swapValue = array(minimum)
swapName = names(minimum)
swapTeam = teams(minimum)
array(minimum) = array(i)
names(minimum) = names(i)
teams(minimum) = teams(i)
array(i) = swapValue
names(i) = swapName
teams(i) = swapTeam
End If