2016-02-26 153 views
0

嗨我创建了一个项目的程序,我现在已经开始运行一些测试,用户的分数计算不正确,我相信它不能比较答案给予正确的答案。我很困惑,需要任何帮助。我的代码看起来像这样,任何令人困惑的部分,我会尝试解释。得分计算不正确

Imports System.IO 

Public Class QuestionScreen 

Dim score As Integer = 0 
Dim count As Integer 
Dim Difficulty_ext As String 
Dim questions, answers As New List(Of String)() 
Private i As Integer 

Sub ReadFile() 

    If Main.Diff_DDown.Text = "Easy" Then 
     Difficulty_ext = "questions - Easy" 
    ElseIf Main.Diff_DDown.Text = "Medium" Then 
     Difficulty_ext = "questions - Medium" 
    Else 
     Difficulty_ext = "questions - Difficult" 
    End If 

    Randomize() 
    Dim countline = File.ReadAllLines("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt").Length 
    Dim numline As Integer 
    Dim values() As String 

    Using sr As New StreamReader("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt") 
     While Not sr.EndOfStream 
      values = sr.ReadLine().Split(","c) 
      questions.Add(values(0)) 
      answers.Add(values(1)) 
     End While 
    End Using 

    numline = Int(Rnd() * countline) 
    For i As Integer = 0 To numline 
     Question.Text = questions(i) 
     Act_ANS.Text = answers(i) 
    Next 
End Sub 

Private Sub Pass_Btn_Click(sender As Object, e As EventArgs) Handles Pass_Btn.Click 
    If count < 10 Then 
     Call ReadFile() 
     count = count + 1 
     Ans_TxtBx.Text = "" 
     Hid_Score.Text = score 
    Else 
     ResultsScreen.Show() 
     Me.Hide() 
    End If 
End Sub 

Public Sub Submit_Btn_Click(sender As Object, e As EventArgs) Handles Submit_Btn.Click 
    If count < 10 Then 
     Call ReadFile() 
     count = count + 1 
     If Ans_TxtBx.Text = answers(i) Then 

      score = score + 1 
     End If 
     Hid_Score.Text = score 
    Else 
     ResultsScreen.Show() 
     Me.Hide() 
     count = 0 
    End If 
    Ans_TxtBx.Text = "" 
End Sub 

Private Sub QuestionScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Call ReadFile() 
End Sub 
End Class 
+0

您的代码中有很多错误,但我认为最主要的是“私人我作为整数”从未初始化。你读它来比较你的答案,但你从来没有把它放在任何地方...... –

回答

0

OK .....试试这个,我已经调试你的代码试图为您把它几乎离开它,有一些问题,没有大错的代码只需要几行地方....

Imports System.IO 

Public Class Form1 

Dim score As Integer = 0 
Dim count As Integer 
Dim Difficulty_ext As String 
Dim questions, answers As New List(Of String)() 
Private i As Integer 

Sub ReadFile() 

    If Diff_DDown.Text = "Easy" Then 
     Difficulty_ext = "questions - Easy" 
    ElseIf Diff_DDown.Text = "Medium" Then 
     Difficulty_ext = "questions - Medium" 
    Else 
     Difficulty_ext = "questions - Difficult" 
    End If 

    Randomize() 
    Try 

     Dim countline = File.ReadAllLines("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt").Length 
     Dim numline As Integer 
     Dim values() As String 

     ' clear the list of questions and answers 
     answers.Clear() 
     questions.Clear() 
     ''''''''''''''''''''''''''''''''''''''''' 

     Using sr As New StreamReader("c:\Users\Alice\Desktop\programme files\" & Difficulty_ext & ".txt") 
      While Not sr.EndOfStream 
       values = sr.ReadLine().Split(","c) 
       questions.Add(values(0)) 
       answers.Add(values(1)) 
      End While 
     End Using 

     numline = Int(Rnd() * countline) 
     For i = 0 To numline 
      Question.Text = questions(i) 
      Act_ANS.Text = answers(i) 
     Next 
    Catch ex As Exception 

    End Try 
End Sub 

Private Sub Pass_Btn_Click(sender As Object, e As EventArgs) Handles Pass_Btn.Click 
    If count < 10 Then 
     count = count + 1 
     Ans_TxtBx.Text = "" 
     Hid_Score.Text = score 
    Else 
     ResultsScreen.Show() 
     Me.Hide() 
    End If 
    Call ReadFile() ' move this to the bottom 
End Sub 

Public Sub Submit_Btn_Click(sender As Object, e As EventArgs) Handles Submit_Btn.Click 
    If count < 10 Then 
     count = count + 1 
     If Ans_TxtBx.Text = answers(i - 1) Then ' need to subtract 1 here 

      score = score + 1 
     End If 
     Hid_Score.Text = score 
    Else 
     ResultsScreen.Show() 
     Me.Hide() 
     count = 0 
    End If 
    Ans_TxtBx.Text = "" 
    Call ReadFile() ' move this to the bottom 
End Sub 

Private Sub QuestionScreen_Load(sender As Object, e As EventArgs) Handles MyBase.Load 
    Call ReadFile() 
End Sub 

End Class 
+0

嗨,我可以问为什么有必要减1?如果你能解释一下,谢谢 –

+0

@Alice Hartley是的,我现在看到这是你原来的问题,我不想过多地改变你的代码,我也很着急,ReadFile中的For-Next循环sub比numline增加'i'1 ...例如。如果numline = 4,那么'i'将在for \ next循环出口之前循环到= 5,然后在使用'i'的值检查的Submit_Btn_Click事件中不匹配正确答案... – Monty

+0

是的,现在出现了一个代码被黄色突出显示的错误,它是这样说的:“索引超出范围,必须是非负数,小于集合的大小。”但由于该文件没有0值,所以我有点困惑,为什么它会抛出这个错误 –