VB.NET文本分数程序运行时错误
我需要创建一个程序,从用户那里获取测试分数,存储分数和分数数,计算平均分数,并确定和显示最高分数。它还有一个按钮,可以清除所有值并重置。我已经为这个程序编写了代码,它似乎没有任何编译器错误。然而,当我运行程序并输入分数时,我得到一个异常。这是留言 应用程序中出现未处理的异常。如果单击“继续”,应用程序将忽略此错误并尝试继续。如果单击“退出”,应用程序将立即关闭 索引超出范围。必须为非负且小于集合的大小。 参数名称:索引。“ 我投入多少并不重要。我一直得到这个例外 我的代码在下面。有人能告诉我我做错了什么吗?谢谢 公开课表格1VB.NET文本分数程序运行时错误,vb.net,Vb.net,我需要创建一个程序,从用户那里获取测试分数,存储分数和分数数,计算平均分数,并确定和显示最高分数。它还有一个按钮,可以清除所有值并重置。我已经为这个程序编写了代码,它似乎没有任何编译器错误。然而,当我运行程序并输入分数时,我得到一个异常。这是留言 应用程序中出现未处理的异常。如果单击“继续”,应用程序将忽略此错误并尝试继续。如果单击“退出”,应用程序将立即关闭 索引超出范围。必须为非负且小于集合的大小。 参数名称:索引。“ 我投入多少并不重要。我一直得到这个例外 我的代码在下面。有人能告诉我我做
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
末级应:
For dTestScore = 0 To ANumberOfScores.Count - 1
ScoreTotal = ScoreTotal + ANumberOfScores(iNumberOfScores)
txtAverageScores.Text = ScoreTotal / iNumberOfScores
Next
阅读:
iNumberOfScores
=1比ANumberOfScores.Count
(来自您发布的代码)多,因此您得到了异常。dtestscores在您的点击事件中始终为零。哇。我真的很震惊++1
编译。显然,编译器将其视为仅1
,但它似乎无效。有人知道为什么它认为这是正确的吗?@Stevendogart我认为这只是把它设定为积极的--1实际上是正的,而--1又回到负的。@LarsTech!正确的。。。我从来没有想过用+
来表示肯定。我只是问了个问题。如果你能回答我的问题,我很乐意给你一个肯定:)
For dTestScore = 0 To ANumberOfScores.Count - 1
ScoreTotal = ScoreTotal + ANumberOfScores(dTestScore)
txtAverageScores.Text = ScoreTotal / iNumberOfScores
Next