我需要创建一个程序,该程序将从用户获取测试分数,存储分数和分数,计算平均分数,以及确定和显示最高分数。它还有一个按钮,用于清除所有值并重置。我已经为程序编写了代码,但它似乎没有任何编译器错误。但是,当我运行程序并输入分数时,我得到一个例外。这是消息....
"您的应用程序中发生了未处理的异常。如果单击“继续”,应用程序将忽略此错误并尝试继续。如果单击“退出”,应用程序将立即关闭。
指数超出范围。必须是非负数且小于集合的大小。 参数名称:index。"
我投入的价值并不重要。我一直得到这个例外
我的代码在下方。有人能告诉我我做错了什么吗?谢谢!
Public Class Form1
Inherits System.Windows.Forms.Form
Private Sub btnEnter_Click(sender As Object, e As EventArgs) Handles btnEnter.Click
Dim dTestScore As Decimal
If IsNumeric(dTestScore >= 0) Then
Call Scores()
Else
MessageBox.Show("Please enter a positive numeric value. ", "User Error")
End If
End Sub
Sub Scores()
Dim ANumberOfScores As New ArrayList
Dim iNumberOfScores As Integer
Dim dTestScore As Decimal
Dim ScoreTotal As Decimal
For Each dTestScore In ANumberOfScores
iNumberOfScores = ++1
txtNumberofScores.Text = iNumberOfScores
Next
For dTestScore = 0 To ANumberOfScores.Count - 1
ScoreTotal = ScoreTotal + ANumberOfScores(iNumberOfScores)
txtAverageScores.Text = ScoreTotal / iNumberOfScores
Next
ANumberOfScores.Sort()
ANumberOfScores.Reverse()
Dim MaxScore = ANumberOfScores(0)
txtBestScore.Text = MaxScore
End Sub
Private Sub btnReset_Click(sender As Object, e As EventArgs) Handles btnReset.Click
txtAverageScores.Clear()
txtBestScore.Clear()
txtNumberofScores.Clear()
txtTestScore.Clear()
End Sub
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
结束班
答案 0 :(得分:0)
应:
For dTestScore = 0 To ANumberOfScores.Count - 1
ScoreTotal = ScoreTotal + ANumberOfScores(iNumberOfScores)
txtAverageScores.Text = ScoreTotal / iNumberOfScores
Next
读:
For dTestScore = 0 To ANumberOfScores.Count - 1
ScoreTotal = ScoreTotal + ANumberOfScores(dTestScore)
txtAverageScores.Text = ScoreTotal / iNumberOfScores
Next
iNumberOfScores
= 1 {超过ANumberOfScores.Count
(来自您发布的代码),因此您获得了例外。