Всякий раз, когда я хочу узнать оценку в программе, которую я сделал.
Он выделяет утверждение k = k + 1
и говорит:
Arithmetic operation resulted in an overflow
Кто-нибудь может помочь?
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
Ваш вклад не то, что вы ожидали. В строке из вашего файла нет «:» или «,», что приводит к бесконечному циклу и, в конечном итоге, к ошибке, когда счетчик превышает максимальное значение. Вы можете использовать Строка.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
Большое спасибо за то, что развеяли мои сомнения. Бог благословил!