函数getQuiz()接受2个参数:行号和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
答案 0 :(得分:0)
你可以使用你的函数getQuiz()的返回值,你现在什么都不做
'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关于你的应用程序工作流程的评论,因为每次调用getQuiz
时加载整个测验内容在这里是非常多余的。
答案 1 :(得分:0)
为什么不
'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