I cannot figure out why my calculation is not working. I am doing for 1 to 12 and it gives me 12 inputs but counts my entries as 13? What am I missing.
If I change it to 0 to 11 same thing. I am not sure what the issue is, but I cannot see it, and am not sure where to look.
I need to end up with 12 cycles through and intEntries to be 12… Help! Thanks!
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
As per my answer to your last question on this topic, whenever the loop hits the line:
…it increments the counter
decEntries.So by the time it’s gone round the loop for the 12th time, it finally comes to
Nextwhich adds 1 more to make the variable’s value13. It then exits the loop because it has exceeded 12.You could rewrite it using a
Whileloop, which I’ll let you investigate – or change your logic to take the finalNextinto account.