vb.net 计算 ListBox 中项目的总和 - Visual Basic

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/17285549/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-17 14:01:21  来源:igfitidea点击:

Calculating the sum of items in a ListBox- Visual Basic

vb.net

提问by CRunge

this is my first time posting on here. I'm having some difficulty with a program for a class I'm taking. I need to get the miles and gallons then calculate the MPG for each entry. I have this part figured out. What I can't seem to get is the totals at the bottom. I need to add the sum of all the items in each ListBox and calculate the total MPG each time the button is clicked. Here's my code so far.

这是我第一次在这里发帖。我在我正在上课的课程中遇到了一些困难。我需要获取英里数和加仑数,然后计算每个条目的 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

Thanks in advance for any help you can provide.

预先感谢您提供的任何帮助。

回答by Tim Schmelter

Your whileloop conditions are incorrect since you are comparing the ListBox.Items.Countwith the total sum. I would use Linqinstead because it's much more readable:

您的while循环条件不正确,因为您将ListBox.Items.Count与总和进行比较。我会改用Linq它,因为它更具可读性:

Dim totalMiles As Int32 = milesListBox.Items.Cast(Of Int32)().Sum()
Dim totalGallons As Int32 = gallonsListBox.Items.Cast(Of Int32)().Sum()