2017-10-20 176 views
0

我試圖編寫的程序假設將由巴士公司使用。該公司有18輛巴士,每輛巴士的用戶必須能夠通過輸入框輸入巴士上有多少人。不斷重置循環內的最小值和最大值vb.net

如果有超過55人乘坐公共汽車,那麼公共汽車會被分類爲滿。當收集所有18輛公交車的數據時,必須輸出每輛公共汽車的平均乘客數量以及最小和最大數量。

這裏是我到目前爲止編碼:

Public Class FirstBus 
    Dim passengers As Integer 
    Dim TotalPassengers As Integer 
    Dim bus As Integer 
    Dim FullBus As Integer 
    Dim min As Integer 
    Dim max As Integer 
    Dim average As Decimal 

    Private Sub FirstBus_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    average = TotalPassengers/18 
    lblOutput.Text = "Average number of passengers = " & average & vbNewLine & "Maxium number of passengers = " & max & vbNewLine & "Minimum number of passengers = " & min 
    bus = 1 
    min = 1000 
    max = 1 
    End Sub 

    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click 
    For i = 1 To 18 
     passengers = InputBox("Enter the number of passengers on bus number " & bus) 
     If passengers > 54 Then 
      FullBus = FullBus + 1 
     End If 
     If passengers > max Then 
      max = passengers 
     ElseIf passengers < min Then 
      min = passengers 
     End If 
     TotalPassengers = TotalPassengers + passengers 
     bus = bus + 1 
    Next i 
    End Sub 

的問題是,當我輸入的所有18條總線的數據,平均值,最小值和最大值都輸出爲0,我需要找到一種方式輸出這些作爲實際的平均值,最小值和最大值

+2

我不知道,如果這兩個方法都以相同的形式,但只是看着它,負載之前執行這些值按鈕按下。所以你也應該按下按鈕後顯示信息。 –

+0

一切都在一種形式,我已經把負載移動到正確的位置,現在的代碼是好的,我踢我自己不接受這個 –

回答

0

你應該在按鈕邏輯完成之前初始化,按鈕邏輯完成後顯示。

我會是這個樣子:

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click 
    ' Reset variables 
    TotalPassengers = 0 
    bus = 0 
    FullBus = 0 
    min = 1000 
    max = 0 
    average = 0 

    ' Get values 
    For i = 1 To 18 
     passengers = InputBox("Enter the number of passengers on bus number " & bus) 
     If passengers > 54 Then 
      FullBus = FullBus + 1 
     End If 
     If passengers > max Then 
      max = passengers 
     ElseIf passengers < min Then 
      min = passengers 
     End If 
     TotalPassengers = TotalPassengers + passengers 
     bus = bus + 1 
    Next i 

    ' Display values 
    average = TotalPassengers/18 
    lblOutput.Text = "Average number of passengers = " & average & vbNewLine & "Maxium number of passengers = " & max & vbNewLine & "Minimum number of passengers = " & min 
End Sub 

負載一次加載時,窗體時,纔會執行。

0

單擊按鈕時,您不刷新lblOutput.Text

Public Class FirstBus 

    Dim passengers As Integer 
    Dim TotalPassengers As Integer 
    Dim bus As Integer 
    Dim FullBus As Integer 
    Dim min As Integer 
    Dim max As Integer 
    Dim average As Decimal 

    Private Sub FirstBus_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
     average = 0 
     min = 1000 
     max = 1 
     lblOutput.Text = "Average number of passengers = " & average & vbNewLine & "Maxium number of passengers = " & max & vbNewLine & "Minimum number of passengers = " & min 
    End Sub 


    Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click 

    TotalPassengers = 0 
    passengers = 0 
    average = 0 
    FullBus = 0 
    bus = 1 
    min = 1000 
    max = 1 
     For i = 1 To 18 
      passengers = InputBox("Enter the number of passengers on bus number " & bus) 
      If passengers > 54 Then 
       FullBus = FullBus + 1 
      End If 
      If passengers > max Then 
       max = passengers 
      ElseIf passengers < min Then 
       min = passengers 
      End If 
      TotalPassengers = TotalPassengers + passengers 
      bus = bus + 1 
     Next i 
     average = TotalPassengers/18 
     lblOutput.Text = "Average number of passengers = " & average & vbNewLine & "Maxium number of passengers = " & max & vbNewLine & "Minimum number of passengers = " & min 
    End Sub 

End Class 
0

您可以在DataGridView中顯示所有公交車,並在那裏更新他們的數據。

Public Class Bus 
    Public Property Number As String 
    Public Property PassengersAmount As Integer 
End Class 

然後創建總線集合並將其綁定到DataGridView
在窗體

Public Sub New() 
    Me.InitializeComponents() 

    Dim buses = Enumerable.Range(1, 18). 
          Select(Function(n) New Bus With { .Number = $"Number {n}"}). 
          ToList() 

    ' Add DataGridView control in designer 
    yourDataGridView.DataSource = buses 
End Sub 

當打開形式寫乘客達到的datagridview的第二列中。
使用LINQ方法計算平均值,最小值和最大值。
注意你不需要在類中的任何私有變量

Private Sub btnStart_Click(sender As Object, e As EventArgs) Handles btnStart.Click 
    Dim buses = DirectCast(yourDataGridView.DataSource, List(Of Bus)) 
    Dim amounts = buses.Select(Function(bus) bus.PassengersAmount).ToList() 

    ' You can create three labels for every output value 
    lblOutputAvg.Text = $"Average: {amounts.Avg()}" 
    lblOutputMin.Text = $"Min: {amounts.Min()}" 
    lblOutputMax.Text = $"Max: {amounts.Max()}" 
End Sub 
相關問題