ここに投稿するのはこれが初めてです。受講しているクラスのプログラムに問題があります。マイルとガロンを取得してから、各エントリの MPG を計算する必要があります。私はこの部分を理解しました。私が得られないように見えるのは、一番下の合計です。ボタンがクリックされるたびに、各 ListBox 内のすべての項目の合計を追加し、MPG の合計を計算する必要があります。これまでの私のコードは次のとおりです。
Public Class MilesPerGallon
Private Sub calculateMPGButton_Click(sender As System.Object,
ByVal e As System.EventArgs) Handles calculateMPGButton.Click
Dim miles As Double ' miles driven
Dim gallons As Double ' gallons used
Dim totalMiles As Double ' total miles driven
Dim totalGallons As Double ' total gallons used
Dim counter As Integer
miles = milesDrivenTextBox.Text ' get the miles driven
gallons = gallonsUsedTextBox.Text ' get the gallons used
If milesDrivenTextBox.Text <> String.Empty Then
' add miles to the end of the milesListBox
milesListBox.Items.Add(milesDrivenTextBox.Text)
milesDrivenTextBox.Clear() ' clears the milesDrivenTextBox
End If
If gallonsUsedTextBox.Text = 0 Then
' do not divide by 0 and alert 0
gallonsUsedTextBox.Text = "Cannot equal 0"
End If
If gallonsUsedTextBox.Text > 0 Then
' add gallons to the end of the gallonsListBox
gallonsListBox.Items.Add(gallonsUsedTextBox.Text)
mpgListBox.Items.Add(String.Format("{0:F}", miles / gallons))
gallonsUsedTextBox.Clear() ' clears the gallonsUsedTextBox
End If
totalMiles = 0
totalGallons = 0
counter = 0
Do While totalMiles < milesListBox.Items.Count
miles = milesListBox.Items(totalMiles)
totalMiles += miles
counter += 1
Do While totalGallons < gallonsListBox.Items.Count
gallons = gallonsListBox.Items(totalGallons)
totalGallons += gallons
counter += 1
Loop
Loop
If totalMiles <> 0 Then
totalResultsLabel.Text = "Total miles driven: " & totalMiles & vbCrLf &
"Total gallons used: " & totalGallons & vbCrLf & "Total MPG: " &
String.Format("{0:F}", totalMiles / totalGallons)
End If
End Sub
End Class
ご協力いただきありがとうございます。