是的,这是家庭作业的一部分。这就是麻烦 - 我知道我可以使用一个阵列,但我们还没有达到课程的这一部分,所以我还不应该使用它。用户输入五个数字(双打,使用输入框),程序应删除最低值并计算剩余四个的平均值。在目前这个时刻,我们一直在研究For ... Next循环,这是本书中相应的For ... Next部分,所以我知道我应该在这里合并一个,但我还没弄明白如何计算,然后保持运行"最低"变量
感谢您的帮助!
原件:
Sub getAverage()
Dim first As Double = CDbl(InputBox("Enter the first grade.", "First"))
Dim second As Double = CDbl(InputBox("Enter the second grade.", "Second"))
Dim third As Double = CDbl(InputBox("Enter the third grade.", "Third"))
Dim fourth As Double = CDbl(InputBox("Enter the fourth grade.", "Fourth"))
Dim fifth As Double = CDbl(InputBox("Enter the fifth grade.", "Fifth"))
Dim min As Double = 0
Dim sum As Double = 0
For count As Integer = 1 To 5
Next
Dim average As Double = 0
txtOutput.Text = average.ToString("N2")
End Sub
编辑1(谢谢Guffa):我应该将当前成绩与?
进行比较Sub getAverage()
Dim min As Double = 0
Dim sum As Double = 0
For count As Integer = 1 To 5
Dim grade As Double = InputBox("Enter Grade #" & count)
sum += grade
If grade < '??? Then
min = grade
End If
Next
Dim average As Double = 0
txtOutput.Text = average.ToString("N2")
End Sub
答案 0 :(得分:1)
不要有五个输入语句,而是在循环中放入一个输入语句。
您需要跟踪:
- The sum of all values
- The lowest value that you encounter
只需在输入时将每个数字添加到总和中,如果输入的值低于之前的值,则更新最低值。
当用户输入所有数字时,您只需从所有值的总和中减去最小数字,这样就可以得到与添加除最低数字之外的所有数字相同的值。
平均值只是总和除以值的数量。