0

我正在尝试编写一个 For... Next 循环,该循环将允许 12 个条目或取消按钮。
不知何故,intEntries 只使用了 1、3、5、7、9 和 11。完成后,计算被除以 13,而不是 12。我不确定我错了什么,但它是显然是什么。非常感谢您能给我的任何帮助!

 Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

        'initialize accumulator
        Dim decEntries As Decimal
        ' For loop to ask for input.
        For decEntries = 1 To 12 Or strMonthlyAvg = " "
            strMonthlyAvg = InputBox("Please Enter the Average for Month # " & decEntries & ":")
            decEntries += 1
            lstTemps.Items.Add(strMonthlyAvg)
            decMontlyAvg = Convert.ToDecimal(strMonthlyAvg)
            ' This will add the montly average to the total Average Temperature for 
            ' calculations
            decTotalTemp += decMontlyAvg

        Next
        ' Calculation to provide the average temp for all entered values
        decAnnualAvg = decTotalTemp / decEntries
        ' convert annual average to string
        strAnnualAvg = Convert.ToString(decAnnualAvg)
        ' Display the results for the user
        lblResults.Text = "The average annual temperature " & vbCrLf &
                            "based on your entries is: " & strAnnualAvg & "."


    End Sub
4

2 回答 2

3

decEntries在 For-Next 循环中有作为计数器,每次Next达到时都会递增。

但是,您也在循环中间“手动”递增它:

decEntries += 1 
于 2012-04-17T14:51:38.750 回答
2

删除decEntries += 1for .. 循环自动加一的行!

于 2012-04-17T14:52:13.053 回答