编码新手。我上周有一个项目要求用户输入4个等级,然后计算所有4个等级的平均值,最低等级的平均值下降(已经转为btw)。要求是For Next循环和Do While循环来计算平均值。我使用For Next循环来获得4个等级的总和,然后计算平均值。
Declare variables, constant, and array
Const intMAX_SUBSCRIPT As Integer = 3
Dim intGrades(intMAX_SUBSCRIPT) As Integer
Dim intTotal As Integer = 0 'holds the total of grades
Dim dblAverage As Double 'holds the average of all 4 grades
Dim intCount As Integer 'loop counter
Dim intLowest As Integer 'holds the lowest score
'assign grades to array slots
intGrades(0) = CInt(txtGrade1.Text)
intGrades(1) = CInt(txtGrade2.Text)
intGrades(2) = CInt(txtGrade3.Text)
intGrades(3) = CInt(txtGrade4.Text)
'Loop to calculate average in grades array
'get total of all grades
For intCount = 0 To (intGrades.Length - 1)
intTotal += intGrades(intCount)
Next
'use floating-point div to find average
dblAverage = intTotal / intGrades.Length
这应该有效。然后我使用以下内容找到最低等级:
intLowest = intGrades(0)
'Search for the lowest grade in the array
For intCount = 1 To (intGrades.Length - 1)
If intGrades(intCount) < intLowest Then
intLowest = intGrades(intCount)
End If
Next
这太有效了。现在我遇到的问题是:得到一个Do While计算新的平均值,得分最低。这是我需要帮助的地方:
Dim intNewTotal As Integer = 0 'holds the new total of the 3 highest grades
Dim dblNewAverage As Double 'holds the new average with the lowest score dropped
Dim intNewCount As Integer = 0 'loop counter
Do While intNewTotal <= (intTotal - intLowest)
intNewTotal = intNewTotal + intGrades.Length - 1
intNewCount += 1
Loop
dblNewAverage = intNewTotal / 3
数学很接近,但并不完全。对于100,100,80和100的成绩,我得到以下结果:所有4的平均值= 95,平均得分最低= 101.新的平均值总是比它应该高一个。我该怎么做才能解决这个问题?我真的很想明白,如果它再次发生,我可以解决这个问题。谢谢!
答案 0 :(得分:1)
问题在于这行代码:
intNewTotal = intNewTotal + intGrades.Length - 1
您可以添加intGrades
数组的长度,而不是添加成绩值,直到达到Do while条件:intNewTotal <= (intTotal - intLowest)
我理解你的问题是出于学术目的,所以如果你真的需要使用Do While循环计算平均值,我会用那种方式(未经测试):
Dim index As Int = 0
Do While index <= intMAX_SUBSCRIPT
If (intGrades(index) <= intLowest) Then
intNewTotal += intGrades(index)
intNewCount += 1
End if
index += 1
Loop
dblNewAverage = intNewTotal / intNewCount
修改强>
降低最低等级可能会以不同的方式解释。如果您具有值80,80,100,110,则可能只想删除80个值中的一个或两个值。我上面的代码正在删除它们。如果您只想删除其中一个,那么您应该将起始索引设置为1以跳过第一个项目并删除if条件:
Dim index As Int = 1
Do While index <= intMAX_SUBSCRIPT
intNewTotal += intGrades(index)
index += 1
Loop
dblNewAverage = intNewTotal / (intGrades.Length - 1)
答案 1 :(得分:1)
这可能是家庭作业,在这种情况下,你无论如何也不会理解这一点。但是,如果您真的想要了解专业人士如何做到这一点,那么代码看起来会更像这样:
Dim Grades As New List(Of Integer) From {CInt(txtGrade1.Text), CInt(txtGrade2.Text), CInt(txtGrade3.Text), CInt(txtGrade4.Text)}
Dim Total As Integer = Grades.Sum()
Dim Average As Double = Grades.Average() 'holds the average of all 4 grades
Dim Lowest As Integer = Grades.Min() 'holds the lowest score
Dim NewAverage As Double = CDbl(Total - Lowest) / (Grades.Count - 1)
并在您的变量名称上删除愚蠢的类型前缀。在Option Strict
是默认值之前,它们曾经很受欢迎并且有意义,但现在甚至微软自己的风格指南都建议不要使用它们。
答案 2 :(得分:0)
Dim j As New List(Of Int32)
Dim sum As Int32 = 0
For i = 1 To 4 '4 number of textboxes
j.Add(CType(Controls.Item("txtGrade" + i.ToString), TextBox).Text)
sum += Val(CType(Controls.Item("txtGrade" + i.ToString), TextBox).Text)
Next
j.Sort()
Dim intLowest As Int32 = j(0)
Dim avg As Int32 = sum / j.Count