VB.NET 文本评分程序运行时错误 [英] VB.NET Text Scores Program RunTime Error
问题描述
我需要创建一个程序,该程序将从用户那里获取测试分数、存储分数和分数数量、计算平均分数以及确定和显示最高分数.它还有一个按钮可以清除所有值并重置.我已经为该程序编写了代码,它似乎没有任何编译器错误.但是,当我运行程序并输入分数时,出现异常.这是消息....
I needed to create a program that will take a Test Score from a user, store the score and the number of scores, calculate the avg score, as well as determine and display the highest score. It also has a button to clear all the values out and reset. I've written code for the program and it doesn't appear to have a any compiler errors. However, when I run the program and enter a score I get an exception. Here is the message....
您的应用程序中发生未处理的异常.如果您单击继续,应用程序将忽略此错误并尝试继续.如果您单击退出,应用程序将立即关闭.
"Unhandled exception has occurred in your application. If you click Continue, the application will ignore this error and attempt to continue. If you click Quit, the application will close immediately.
索引超出范围.必须是非负的并且小于集合的大小.参数名称:索引."
Index was out of range. Must be non-negative and less than the size of the collection. Parameter name: index."
我输入什么值并不重要.我不断收到此异常
It doesn't matter what value I put in. I keep getting this exception
我的代码在下面.有人能告诉我我做错了什么吗?谢谢!
My code is down below. Can someone tell me what I've done wrong? Thanks!
公开课表格1
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
阅读:
For dTestScore = 0 To ANumberOfScores.Count - 1
ScoreTotal = ScoreTotal + ANumberOfScores(dTestScore)
txtAverageScores.Text = ScoreTotal / iNumberOfScores
Next
iNumberOfScores
= 1 比 ANumberOfScores.Count
(来自您发布的代码)因此您得到了例外.
iNumberOfScores
= 1 more than ANumberOfScores.Count
(from your posted code) hence the exception you got.
这篇关于VB.NET 文本评分程序运行时错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!