为什么我的程序没有正确排序?
Why is my program not sorting properly?
我的程序有问题,程序应该询问用户驱动时间,用户将输入驱动时间,程序应该从最快到最慢排序然后显示它们按顺序。
但是当我尝试 运行 程序时,驱动程序时间已排序但驱动程序名称尚未排序。
示例:
用户必须在程序中输入什么。
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
提前致谢
您只是在对 raceTimes 数组进行排序。您需要对 raceCar 变量中的其他数组执行相同的操作,但幸运的是,您可以利用现有的逻辑:
首先,您需要将其他数组传递给 sortArray 方法并为每个数组创建 "Swap Variables":
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
我的程序有问题,程序应该询问用户驱动时间,用户将输入驱动时间,程序应该从最快到最慢排序然后显示它们按顺序。 但是当我尝试 运行 程序时,驱动程序时间已排序但驱动程序名称尚未排序。
示例:
用户必须在程序中输入什么。
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
提前致谢
您只是在对 raceTimes 数组进行排序。您需要对 raceCar 变量中的其他数组执行相同的操作,但幸运的是,您可以利用现有的逻辑:
首先,您需要将其他数组传递给 sortArray 方法并为每个数组创建 "Swap Variables":
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