如何修复错误"Arithmetic operation resulted in an overflow"



每当我想找出我制作的程序中的成绩时。
它突出显示了k = k + 1语句并说:

算术运算导致溢出

有人可以帮忙吗?

Sub SearchStudentData()
    Dim Sname, G As String
    Dim Lname, Lgradetext, position, j, k, position1 As Integer
    Dim gradefile As IO.StreamReader
    Dim Valid As Boolean
    Valid = False
    Console.WriteLine("Enter the name of the student of whom you want the grade!")
    Sname = Console.ReadLine()
    Lname = Len(Sname)
    gradefile = New IO.StreamReader("D:Grades.txt")
    Do Until gradefile.EndOfStream
        gradetext = gradefile.ReadLine()
        Lgradetext = Len(gradetext)
        j = 0
        k = 0
        Do
            k = k + 1      'It highlights this line of code
            position1 = k
        Loop Until Mid(gradetext, k, 1) = ":"
        Do
            j = j + 1
            position = j
        Loop Until Mid(Lgradetext, j, 1) = ","
        If Sname = Right(gradetext, position1 + 1) And Sname = Left(gradetext, position - 1) Then
            Valid = True
        End If
        If Valid = True Then
            G = Right(Lgradetext, Lgradetext - 1)
            Console.WriteLine(G)
        Else
            Valid = False
            Console.WriteLine("Ypu have failed this PROGRAM")
        End If
    Loop
    gradefile.Close()
End Sub

您的输入不是您所期望的。 文件中的行中没有":"或",",导致无限循环,最终在计数器超过最大值时出错。您可以使用 String.IndexOf(( 来确定该值是否存在,而不是循环。 当该值不存在时,将返回 -1。 下面是一个示例:

Dim indexOfColon As Integer = gradetext.IndexOf(":")
Dim indexOfComma As Integer = gradetext.IndexOf(",")
If indexOfColon <> -1 AndAlso indexOfComma <> -1 Then
    ' ... both ":" and "," were present in the string, do something with those values ...
End If

相关内容

最新更新