为什么我的程序在循环结束前崩溃了?

时间:2016-11-09 11:48:23

标签: vb.net

所以我遇到的问题是我的程序似乎在我循环的第二个最后一个增量之后崩溃/没有响应。我的计划是询问用户班级中的学生数量,测试的最高分数,然后循环学生的数量,用户向程序提供学生姓名和他们的考试成绩,然后在其中进行其他计算。 分数的详细信息(名称,标记等)在每个循环结束时的多列列表视图中更新。程序在循环的第一个增量中正常工作但在此之后失败。这是执行它的子代码:

Private Sub btnData_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnData.Click

    Dim NumStudent, HighestMark, LowestMark, ClassTotal, ClassMean, count As Integer
    Dim HscScore As Decimal

    Dim HscBand(5) As Integer
    Dim Band1 As Integer = HscBand(0)
    Dim Band2 As Integer = HscBand(1)
    Dim Band3 As Integer = HscBand(2)
    Dim Band4 As Integer = HscBand(3)
    Dim Band5 As Integer = HscBand(4)
    Dim Band6 As Integer = HscBand(5)

    NumStudent = InputBox("Enter the number of students for this test")
    NumStudent = NumStudent - 1

    MaxScore = InputBox("Enter the maximum score for this test")


    Dim student(NumStudent) As StudentHSC



    For count = 0 To NumStudent
        student(count).StudentName = InputBox("Enter the student's name")

        student(count).StudentScore = InputBox("Enter the student's score for the test")
        If student(count).StudentScore > MaxScore Then
            MessageBox.Show("You can not enter a number higher than the max score, please enter it again")
            student(count).StudentScore = InputBox("Enter the student's score for the test")
        End If

        HscScore = student(count).StudentScore / MaxScore * 100
        student(count).HscMark = Format(HscScore, "0.00")

        If student(count).HscMark < 50 Then
            student(count).HscRank = 1
            HscBand(0) = HscBand(0) + 1
            lstStuData.Items.Add(New ListViewItem(New String() {student(count).StudentName, student(count).StudentScore, student(count).HscMark, student(count).HscRank}))

        ElseIf student(count).HscMark = 50 And student(count).HscMark < 60 Then
            student(count).HscRank = 2
            HscBand(1) = HscBand(1) + 1
            lstStuData.Items.Add(New ListViewItem(New String() {student(count).StudentName, student(count).StudentScore, student(count).HscMark, student(count).HscRank}))

        ElseIf student(count).HscMark = 60 And student(count).HscMark < 70 Then
            student(count).HscRank = 3
            HscBand(2) = HscBand(2) + 1
            lstStuData.Items.Add(New ListViewItem(New String() {student(count).StudentName, student(count).StudentScore, student(count).HscMark, student(count).HscRank}))

        ElseIf student(count).HscMark = 70 And student(count).HscMark < 80 Then
            student(count).HscRank = 4
            HscBand(3) = HscBand(3) + 1
            lstStuData.Items.Add(New ListViewItem(New String() {student(count).StudentName, student(count).StudentScore, student(count).HscMark, student(count).HscRank}))

        ElseIf student(count).HscMark = 80 And student(count).HscMark < 90 Then
            student(count).HscRank = 5
            HscBand(4) = HscBand(4) + 1
            lstStuData.Items.Add(New ListViewItem(New String() {student(count).StudentName, student(count).StudentScore, student(count).HscMark, student(count).HscRank}))

        ElseIf student(count).HscMark = 90 And student(count).HscMark < 101 Then
            student(count).HscRank = 6
            HscBand(5) = HscBand(5) + 1
            lstStuData.Items.Add(New ListViewItem(New String() {student(count).StudentName, student(count).StudentScore, student(count).HscMark, student(count).HscRank}))

        End If







    Next

我已经在这个程序中遇到了太多问题,除了这个问题外,所有问题都解决了。我觉得它与NumStudent变量有关...

StudentHSC也是一项公共职能。

编辑:这是我设置的公共变量和结构以及列表视图格式:

Private Sub Form2_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
    lstStuData.View = View.Details
    lstStuData.Columns.Add("Student Name", 100, HorizontalAlignment.Left)
    lstStuData.Columns.Add("Score", 60, HorizontalAlignment.Left)
    lstStuData.Columns.Add("Percentage", 70, HorizontalAlignment.Left)
    lstStuData.Columns.Add("Band", 60, HorizontalAlignment.Left)


End Sub

Public MaxScore As Integer
Public student As String

Public Structure StudentHSC
    Dim StudentName As String
    Dim StudentScore As Integer
    Dim HscRank As Integer
    Dim HscMark As Integer
End Structure

1 个答案:

答案 0 :(得分:2)

我建议您使用Option Strict On声明进行编码,以免依赖于从一种类型到另一种类型的隐式转换。

您的问题的答案是您已经使用(NumStudents + 1)元素声明了一组StudentHSC对象。 StudentHSC是一个引用类型,因此数组的每个元素都是指向对象的指针。在使用New关键字在堆上创建对象之前,每个指针都不指向任何对象或指向&#34; Nothing&#34;。因此,如果您尝试为元素的属性(例如StudentName)分配值,您将获得&#34; null引用异常&#34;。要解决此问题,请在For循环的顶部插入以下行:

student(count) = New StudentHSC

您还没有显示StudentHSC类,所以我假设它有一个不带参数的构造函数。

所以For循环的开头应该是这样的:

For count = 0 To NumStudent
    student(count) = New StudentHSC
    student(count).StudentName = InputBox("Enter the student's name")

此外,您还有许多未使用或冗余的变量,您应该验证输入框的用户条目,以确保提供了数值。

编辑:

If子句应如下所示:

If student(count).HscMark < 50 Then
    student(count).HscRank = 1
    HscBand(0) = HscBand(0) + 1
    lstStuData.Items.Add(New ListViewItem(New String() {student(count).StudentName, student(count).StudentScore.ToString, student(count).HscMark.ToString, student(count).HscRank.ToString}))
ElseIf student(count).HscMark >= 50 AndAlso student(count).HscMark < 60 Then
    student(count).HscRank = 2
    HscBand(1) = HscBand(1) + 1
    lstStuData.Items.Add(New ListViewItem(New String() {student(count).StudentName, student(count).StudentScore.ToString, student(count).HscMark.ToString, student(count).HscRank.ToString}))
ElseIf student(count).HscMark >= 60 AndAlso student(count).HscMark < 70 Then
    student(count).HscRank = 3
    HscBand(2) = HscBand(2) + 1
    lstStuData.Items.Add(New ListViewItem(New String() {student(count).StudentName, student(count).StudentScore.ToString, student(count).HscMark.ToString, student(count).HscRank.ToString}))
ElseIf student(count).HscMark >= 70 AndAlso student(count).HscMark < 80 Then
    student(count).HscRank = 4
    HscBand(3) = HscBand(3) + 1
    lstStuData.Items.Add(New ListViewItem(New String() {student(count).StudentName, student(count).StudentScore.ToString, student(count).HscMark.ToString, student(count).HscRank.ToString}))
ElseIf student(count).HscMark >= 80 AndAlso student(count).HscMark < 90 Then
    student(count).HscRank = 5
    HscBand(4) = HscBand(4) + 1
    lstStuData.Items.Add(New ListViewItem(New String() {student(count).StudentName, student(count).StudentScore.ToString, student(count).HscMark.ToString, student(count).HscRank.ToString}))
ElseIf student(count).HscMark >= 90 AndAlso student(count).HscMark <= 100 Then
    student(count).HscRank = 6
    HscBand(5) = HscBand(5) + 1
    lstStuData.Items.Add(New ListViewItem(New String() {student(count).StudentName, student(count).StudentScore.ToString, student(count).HscMark.ToString, student(count).HscRank.ToString}))
End If