好的,所以我应该让用户输入最多13个等级,然后在删除两个最低等级后平均。我无法弄清楚要删除两个最低等级的内容,无论是从列表框还是在计算过程中。到目前为止,这是我的代码:
Dim strGrades As String
Dim decGrades As Decimal = 0
Dim decAverage As Decimal = 0
Dim decTotal As Decimal = 0
Dim strInputMessage As String = "Enter grade #"
Dim strInputHeading As String = "Grade"
Dim strNormalMessage As String = "Enter grade #"
Dim strNonNumericError As String = "Error - Please enter a grade"
Dim strNegativeError As String = "Error - Please enter a positive number"
Dim strCancelClicked As String = ""
Dim intMaxNumberOfEntries As Decimal = 13
Dim intNumberOfEntries As Decimal = 1
strGrades = InputBox(strInputMessage & intNumberOfEntries, strInputHeading, " ")
Do Until intNumberOfEntries > intMaxNumberOfEntries Or strGrades = strCancelClicked
If IsNumeric(strGrades) Then
decGrades = Convert.ToDecimal(strGrades)
If decGrades >= 0 Then
lstGrades.Items.Add(decGrades)
decTotal += decGrades
intNumberOfEntries += 1
strInputMessage = strNormalMessage
Else
strInputMessage = strNegativeError
End If
Else
strInputMessage = strNonNumericError
End If
If intNumberOfEntries <= intMaxNumberOfEntries Then
strGrades = InputBox(strInputMessage & intNumberOfEntries, strInputHeading, " ")
End If
Loop
lstGrades.Sorted = True
答案 0 :(得分:1)
假设您有一个可枚举的数值列表,例如一个Decimal
的数组,然后最简洁的方法来得到除了两个最低值之外的所有平均值:
Dim average = myList.OrderBy(Function(n) n).Skip(2).Avg()
我不确定作业中有多少是可以接受的 - 如果这就是这个 - 但是如果你开始的话,你仍然可以通过调用Skip(2)
完成具体问题的部分。按升序排序的列表。
答案 1 :(得分:0)
您可以对列表框中的条目进行计数,并删除底部的2个条目。
您可以将它们添加到列表中并对其进行排序。