0

我无法弄清楚为什么我的计算不起作用。我正在做 1 到 12,它给了我 12 个输入,但我的输入计为 13?我错过了什么。

如果我将其更改为 0 到 11 相同的东西。我不确定问题是什么,但我看不到它,也不知道在哪里看。

我需要以 12 个周期结束,intEntries 为 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 & ":")

            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

1 回答 1

4

根据我对您关于此主题的最后一个问题的回答,每当循环到达该行时:

Next

...它增加了 counter decEntries

因此,当它第 12 次循环循环时,它最终会Next增加 1 以使变量的 value 13。然后它退出循环,因为它已超过 12。

您可以使用While循环重写它,我会让您进行调查 - 或者更改您的逻辑以考虑最终结果Next

于 2012-04-17T16:38:09.743 回答