for ...下次计算在VB中无法正常工作 - 家庭作业

时间:2012-04-17 16:29:54

标签: vb.net loops

我无法弄清楚为什么我的计算不起作用。我正在做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

1 个答案:

答案 0 :(得分:4)

根据我对your last question关于此主题的回答,每当循环到达该行时:

Next

...它会增加计数器decEntries

所以当它第12次绕过循环时,它终于来到Next,它增加了1以使变量的值13。然后退出循环,因为它超过了12。

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