2017-03-08 81 views
0

我的程序有問題,程序應該詢問用戶的驅動程序時間,用戶將輸入驅動程序的時間,程序應該從最快到最慢排序,然後按順序顯示它們。 但是,當我嘗試並運行該程序時,drvers時間已排序,但驅動程序名稱尚未排序。爲什麼我的程序沒有正確排序?

示例:

用戶必須輸入到程序中的內容。

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

是代碼在VB?如果有的話,請在標籤中註明 – MaxZoom

回答

1

你只排序raceTimes陣列。您需要爲您的raceCar變量中的其他陣列執行相同的操作,但幸運的是,您可以從現有邏輯中捎帶回來:

首先,您需要將其他數組傳遞給sortArray方法,並創建「Swap變量」每個:

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 
相關問題