在函数循环vb.net中传递变量值

在函数循环vb.net中传递变量值,vb.net,function,loops,variables,Vb.net,Function,Loops,Variables,函数getquick()接受两个参数,即csv文件的行号和路径。它将问题和选项分开,然后对照正确答案进行检查,正确答案很好。我希望能够在测验结束时返回分数。问题在于我正在循环函数。每次得分都会重置 如何在测验结束时检索分数? 函数的调用方式如下: 'Calls the getQuiz function Sub Main() Dim i As Integer For i = 2 To 6 getQuiz(i, "C:\Users\HisEasy.csv")

函数getquick()接受两个参数,即csv文件的行号和路径。它将问题和选项分开,然后对照正确答案进行检查,正确答案很好。我希望能够在测验结束时返回分数。问题在于我正在循环函数。每次得分都会重置

如何在测验结束时检索分数? 函数的调用方式如下:

'Calls the getQuiz function
Sub Main()
    Dim i As Integer

    For i = 2 To 6
        getQuiz(i, "C:\Users\HisEasy.csv")   
    Next

End Sub


Function getQuiz(ByVal lineNumber As Integer, ByVal filename As String) As String
    Dim allLines As List(Of String) = New List(Of String)
    Dim qQuest, qOption1, qOption2, qOption3, answer
    Dim quizVar

    Dim n As Long
    Dim score As long
    score = 0
    Using reader As New System.IO.StreamReader(filename)
        For i As Integer = 1 To lineNumber - 1
            If reader.ReadLine() Is Nothing Then
                Throw New ArgumentOutOfRangeException("lineNumber")
            End If
        Next
        ' Attempts to read the line you've requested '
        Dim line As String = reader.ReadLine()
        If line Is Nothing Then
            Throw New ArgumentOutOfRangeException("lineNumber")
        End If

        line = Replace(line, Chr(34), "")
        quizVar = Split(line, ",")

        qQuest = quizVar(1)
        qOption1 = quizVar(2)
        qOption2 = quizVar(3)
        qOption3 = quizVar(4)

        Console.WriteLine(qQuest)
        Console.WriteLine(qOption1)
        Console.WriteLine(qOption2)

        answer = Console.ReadLine()
        If answer = qOption3 Then
            Console.WriteLine("Correct Answer!")
            Console.ReadLine()
            score = n + 1
            Return score
        End If

    End Using

End Function

您可以使用函数getquick()的返回值,目前您对该函数不做任何操作

'Calls the getQuiz function
Sub Main()
    Dim i As Integer
    Dim score as Integer

    For i = 2 To 6
        If getQuiz(i, "C:\Users\HisEasy.csv") = True then
            score += 1
        End If
    Next

End Sub


Function getQuiz(ByVal lineNumber As Integer, ByVal filename As String) As Boolean
    Dim allLines As List(Of String) = New List(Of String)
    Dim qQuest, qOption1, qOption2, qOption3, answer
    Dim quizVar

    Dim n As Long
    Dim score As long

    Using reader As New System.IO.StreamReader(filename)
        For i As Integer = 1 To lineNumber - 1
            If reader.ReadLine() Is Nothing Then
                Throw New ArgumentOutOfRangeException("lineNumber")
            End If
        Next
        ' Attempts to read the line you've requested '
        Dim line As String = reader.ReadLine()
        If line Is Nothing Then
            Throw New ArgumentOutOfRangeException("lineNumber")
        End If

        line = Replace(line, Chr(34), "")
        quizVar = Split(line, ",")

        qQuest = quizVar(1)
        qOption1 = quizVar(2)
        qOption2 = quizVar(3)
        qOption3 = quizVar(4)

        Console.WriteLine(qQuest)
        Console.WriteLine(qOption1)
        Console.WriteLine(qOption2)

        answer = Console.ReadLine()
        If answer = qOption3 Then
            Console.WriteLine("Correct Answer!")
            Console.ReadLine()
            Return True
        End If

    End Using

End Function

你也应该认真考虑一下Andrew Morton关于你的应用程序工作流的评论,因为每次调用<代码> > GQuiZix/Cux>时,整个测试内容都是非常多余的。

< P>为什么不只是

'Calls the getQuiz function
Sub Main()
    Dim i As Integer
    Dim score as long

    score = 0

    For i = 2 To 6
        'Add score after each call
        score = score + getQuiz(i, "C:\Users\HisEasy.csv")   

    Next

End Sub

'Use Long instead of String as return type
Function getQuiz(ByVal lineNumber As Integer, ByVal filename As String) As long
    Dim allLines As List(Of String) = New List(Of String)
    Dim qQuest, qOption1, qOption2, qOption3, answer
    Dim quizVar

    Dim n As Long
    Dim score As long
    score = 0
    Using reader As New System.IO.StreamReader(filename)
        For i As Integer = 1 To lineNumber - 1
            If reader.ReadLine() Is Nothing Then
                Throw New ArgumentOutOfRangeException("lineNumber")
            End If
        Next
        ' Attempts to read the line you've requested '
        Dim line As String = reader.ReadLine()
        If line Is Nothing Then
            Throw New ArgumentOutOfRangeException("lineNumber")
        End If

        line = Replace(line, Chr(34), "")
        quizVar = Split(line, ",")

        qQuest = quizVar(1)
        qOption1 = quizVar(2)
        qOption2 = quizVar(3)
        qOption3 = quizVar(4)

        Console.WriteLine(qQuest)
        Console.WriteLine(qOption1)
        Console.WriteLine(qOption2)

        answer = Console.ReadLine()
        If answer = qOption3 Then
            Console.WriteLine("Correct Answer!")
            Console.ReadLine()
            score = n + 1
            Return score
        End If

    End Using

End Function

您可以在
Main
部分中添加分数。只需从
getquick
函数中根据需要返回0或1即可。另外,您可能会对一个函数感兴趣,它可以一次性将文件中的所有行读取到一个数组中,这样您就不必为每个问题一直读取该文件。简单地将
score
变量的声明移到
getquick
方法之外,这不是一件显而易见的事情吗?