搜索数组中的特定数字

时间:2015-05-28 15:18:23

标签: vb.net

我正在编写一个需要在am Array中搜索特定数字范围的程序。我需要搜索的数字实际上是学生可以获得的成绩,即As,Bs,Cs,Ds,Es和Fs。这是我的代码:

导入System.IO Imports System.Math

公共类frmClassStatistics

'Arrays to hold the name and marks read from file 
Private strName() As String
Private dblMark() As Integer
Private intNumStudents As Integer = 0
Private Average As Double
Private StandardDeviation As Double
Private arrHighestMark() As Double

Private Sub frmClassStatistics_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    'Reads each student name and final mark from the mark file. It them separates each name from the mark
    'and places the student names and the final mark into two, one-dimensional arrays. 
    'One array will be an array of string (for the names) and the other array will be an array of real numbers (for the marks).

    Dim strFilename As String
    Dim i As Integer
    Dim intLastBlank As Integer
    Dim strInput As String
    Dim StudentMarks = "C:\StudentMarks.txt"

    strFilename = "StudentMarks.txt"

    If File.Exists(strFilename) Then
        Dim srReader As New StreamReader(strFilename)

        Try

            i = 0

            While Not srReader.EndOfStream

                strInput = srReader.ReadLine()

                'find location of TAB between the name and the mark.
                intLastBlank = strInput.IndexOf(vbTab)

                'take all characters before the TAB and place them into the name array
                strName(i) = strInput.Substring(0, intLastBlank)

                intNumStudents = intNumStudents + 1

                'take all characters after the TAB, convert them into a real number and
                'place them into the mark array
                dblMark(i) = Double.Parse(strInput.Substring(intLastBlank + 1))

                i = i + 1

            End While

        Catch ex As Exception
            MessageBox.Show("Problem reading data from the file")
        End Try

        srReader.Close()  ' Close the file

    End If

End Sub


Private Sub btnShowStatistics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnShowStatistics.Click

    'Finds the dblMark array to find the class avarage, highest Mark and number of students above the class avarage

  'I earased it for simplicity

    Grades()

End Sub

Private Sub Grades()

    Dim blnFound As Boolean = False
    Dim intCount As Integer = 0
    Dim intPosition As Integer

'在我运行程序的那一刻,我在这里得到一个错误,说没有处理null引用异常

    Do While blnFound And intCount < dblMark.Length
        If dblMark(intCount) >= 98 Then
            blnFound = True
            intPosition = intCount
        End If
        intCount += 1
    Loop
    'check to see if value found
    If blnFound Then
        txtAs.Text = intPosition
    Else
        txtAs.Text = 0
    End If

End Sub

我是否使用这种循环完成基地?有没有更简单的方法来编程呢?

1 个答案:

答案 0 :(得分:1)

首先,您从未调整dblMark()的大小。所以我很惊讶你在加载dblMark的循环中没有出错。这使我认为您的If FileExists()找不到该文件,因此会跳过所有代码。

错误最终会发生,因为您的变量dblMarknothing。如果您为其指定了值或将其声明为dblMark(0),则您的错误会消失,但您的程序可能仍然无法按照您的意愿执行操作。尝试使用strFilename的完整路径(就像您在上面声明了两行:strFilename = "c:\studentmarks.txt

其他观察:你的变量dblMark()被声明为一个整数数组,但是你正在解析为double(名字暗示是double,而不是整数)。