从数据库中检索数据的多项选择测验 - 循环问题的问题

时间:2016-04-01 19:07:17

标签: vb.net

我的多项选择测验出了问题。当我点击下一步并返回时,它没有显示数据库中的不同问题,无线电按钮和标签不会改变以显示不同的问题或答案。对不起,如果这令人困惑。

代码低于

Option Strict On
Imports System.Data.OleDb
Public Class Form1
Dim ConnectString As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & _
  " Data Source= C:\Users\Sales\Documents\Visual Studio 2010\Normalised Database.accdb"

Dim dr As OleDbDataReader
Dim cm As New OleDbCommand
Dim cn As New OleDbConnection
Dim provider As String
Dim dataFile As String
Dim no As Integer
'    Dim q(no) As String
Dim quesnum As Integer = 1
Dim answer As String
Dim selected As String
Dim score As Integer = 0

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

    no = CInt(InputBox("Input the number of questions you want. You can choose from 1 to 10."))
    QuesNo.Hide()
    QuesLabel.Hide()
    Label3.Hide()
    RadioButton1.Hide()
    RadioButton2.Hide()
    RadioButton3.Hide()
    RadioButton4.Hide()
    NextBtn.Hide()
    BackBtn.Hide()
    BackBtn.Hide()
    SaveBtn.Hide()
    ReturnBtn.Hide()

    For i = 1 To no
        question()
        mark()

    Next

    'End
End Sub

Private Sub question()

    cn.ConnectionString = ConnectString
    cn.Open()

    ' quesnum = 0
    '   quesnum = quesnum + 1
    QuesNo.Text = "Question " & quesnum & " of " & no

    cm.CommandText = "SELECT Question, Answer1, Answer2, Answer3, CorrectAnswer FROM Question"
    cm.Connection = cn

    dr = cm.ExecuteReader

    Dim answerList As New List(Of String)

    If dr.HasRows Then
        dr.Read()
        QuesLabel.Text = CStr(dr.Item("Question"))

        answerList.Add(CStr(dr.Item("Answer1")))
        answerList.Add(CStr(dr.Item("Answer2")))
        answerList.Add(CStr(dr.Item("Answer3")))
        answerList.Add(CStr(dr.Item("CorrectAnswer")))
        answerList = RandomizeListOrder(answerList)
        RadioButton1.Text = answerList(0)
        RadioButton2.Text = answerlist(1)
        RadioButton3.Text = answerList(2)
        RadioButton4.Text = answerlist(3)
        dr.Close()
    End If
    cn.Close()

End Sub

Private Sub mark()
    If RadioButton1.Checked = True Then selected = RadioButton1.Text
    If RadioButton2.Checked = True Then selected = RadioButton2.Text
    If RadioButton3.Checked = True Then selected = RadioButton3.Text
    If RadioButton4.Checked = True Then selected = RadioButton4.Text


    cn.ConnectionString = ConnectString
    cn.Open()
    cm.CommandText = "SELECT CorrectAnswer FROM Question"
    cm.Connection = cn
    dr = cm.ExecuteReader
    If dr.HasRows Then
        dr.Read()
        answer = CStr(dr.Item("CorrectAnswer"))
        dr.Close()

    End If
    cn.Close()

    If selected = answer Then
        score = score + 1
    End If

    '   question()

    Label3.Text = "score is " & score

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles NextBtn.Click

    For i = 1 To no
        question()

        mark()

    Next
    quesnum = quesnum + 1
End Sub

Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles StartBtn.Click

    QuesNo.Show()
    QuesLabel.Show()
    Label3.Show()
    RadioButton1.Show()
    RadioButton2.Show()
    RadioButton3.Show()
    RadioButton4.Show()
    NextBtn.Show()
    BackBtn.Show()
    SaveBtn.Show()
    ReturnBtn.Show()
    StartBtn.Hide()

End Sub

Private Function RandomizeListOrder(ByVal answers As List(Of String)) As List(Of String)

    Dim answer2 As Integer
    Dim rnd As New Random
    'loop though each item in the list
    For answer1 As Integer = 0 To answers.Count - 1
        'pick a random answer
        answer2 = rnd.Next(0, answers.Count - 1)
        'If answer1 and answer2 are the same then skip the rest of the code
        'in the loop and go onto the next question
        If answer1 = answer2 Then
            Continue For
        End If
        'swap the questions over
        Dim temp As String
        temp = answers(answer1)
        answers(answer1) = answers(answer2)
        answers(answer2) = temp
    Next
    'return the list of answers
    Return answers

End Function

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BackBtn.Click

End Sub
End Class

1 个答案:

答案 0 :(得分:1)

确定在原始代码中,没有问题编号传递给question()程序。所以,我将声明改为..

Private Sub question(quesnum As Integer)

然后我从Form_Load程序中删除了问题循环,因为它不应该在那里并且应该已经写入mark这样的程序

If quesnum < no Then
        quesnum += 1
End If

我已将question程序中的查询命令更改为

cm.CommandText = "SELECT CorrectAnswer FROM Question WHERE QuestionNo = " & quesnum.ToString

现在根据quesnum变量获得特定问题。

当查询从数据库中获得正确的答案时,我在mark过程中也做了同样的事情。

最后,我在IfBack按钮点击中添加了Next个语句,以确保用户无法超越问题的开头或结尾。

好的那就是它,但肯定还有一些问题

  • 当所有问题都得到解答时,您将如何发现

  • 您如何跟踪哪些问题得到解答? - 目前,用户可以 回答问题1,转到问题2,然后回到问题1并再次回答并获得另一个积分。

  • 最后,您需要添加一项检查,以确保用户输入的数字不会超过数据库中的问题数。

玩得开心 - 希望这一切都有所帮助。以下是完整的代码

Imports System.Data.OleDb
Public Class Form1
    Dim ConnectString As String = "Provider=Microsoft.ACE.OLEDB.12.0;" & " Data Source= C:\Users\Sales\Documents\Visual Studio 2010\Normalised Database.accdb"
    Dim dr As OleDbDataReader
    Dim cm As New OleDbCommand
    Dim cn As New OleDbConnection
    Dim provider As String
    Dim dataFile As String
    Dim no As Integer
    Dim q(no) As String
    Dim quesnum As Integer
    Dim answer As String
    Dim selected As String
    Dim score As Integer

    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        no = CInt(InputBox("Input the number of questions you want. You can choose from 1 to 10."))
        Label1.Hide()
        Label2.Hide()
        Label3.Hide()
        RadioButton1.Hide()
        RadioButton2.Hide()
        RadioButton3.Hide()
        RadioButton4.Hide()
        Button1.Hide()
        Button2.Hide()
        Button2.Hide()
        Button3.Hide()
        Button4.Hide()
    End Sub

    Private Sub question(quesnum As Integer)
        cn.ConnectionString = ConnectString
        cn.Open()
        Label1.Text = "Question " & quesnum & " of " & no
        'your query now selects a specific question number with the addition of WHERE QuestionNo = " & quesnum.ToSring
        cm.CommandText = "SELECT Question, Answer1, Answer2, Answer3, CorrectAnswer FROM Question WHERE QuestionNo = " & quesnum.ToString
        cm.Connection = cn
        dr = cm.ExecuteReader
        Dim answerList As New List(Of String)
        If dr.HasRows Then
            dr.Read()
            Label2.Text = dr.Item("Question").ToString
            answerList.Add(dr.Item("Answer1").ToString)
            answerList.Add(dr.Item("Answer2").ToString)
            answerList.Add(dr.Item("Answer3").ToString)
            answerList.Add(dr.Item("CorrectAnswer").ToString)
            answerList = RandomizeListOrder(answerList)
            RadioButton1.Text = answerList(0)
            RadioButton2.Text = answerList(1)
            RadioButton3.Text = answerList(2)
            RadioButton4.Text = answerList(3)
            dr.Close()
        End If
        cn.Close()
    End Sub

    Private Sub mark()
        'See the function below
        selected = CheckedRadioButtonText()
        cn.ConnectionString = ConnectString
        cn.Open()
        'same again here for selecting the correct answer from the correct question
        cm.CommandText = "SELECT CorrectAnswer FROM Question WHERE QuestionNo = " & quesnum.ToString
        cm.Connection = cn
        dr = cm.ExecuteReader
        If dr.HasRows Then
            dr.Read()
            answer = dr.Item("CorrectAnswer").ToString
            dr.Close()
        End If
        cn.Close()
        'Here is where the point where quesnum is incremented to get ready to load
        'The next question after the current one has been marked
        If selected = answer Then score = score + 1
        MsgBox("score is " & score)
    End Sub

    'This function checks all the radio buttons to see which one is checked
    ' and returns the text of that RadioButton
    Private Function CheckedRadioButtonText() As String
        If RadioButton1.Checked Then
            Return RadioButton1.Text
        ElseIf RadioButton2.Checked Then
            Return RadioButton2.Text
        ElseIf RadioButton3.Checked Then
            Return RadioButton3.Text
        Else
            Return RadioButton4.Text
        End If
    End Function

    Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        mark()
        'move to next question 
        If quesnum < no Then
            quesnum += 1
        End If
        question(quesnum)
    End Sub

Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
    'Move to previous question
    If quesnum > 1 Then
        quesnum -= 1
    End If
    question(quesnum)
End Sub

    Private Sub Button5_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button5.Click
        Label1.Show()
        Label2.Show()
        Label3.Show()
        RadioButton1.Show()
        RadioButton2.Show()
        RadioButton3.Show()
        RadioButton4.Show()
        Button1.Show()
        Button2.Show()
        Button3.Show()
        Button4.Show()
        Button5.Hide()
        'Set the first question from here. 
        quesnum = 1
        question(quesnum)
    End Sub

    Private Function RandomizeListOrder(answers As List(Of String)) As List(Of String)
        Dim answer2 As Integer
        Dim rnd As New Random
        'loop though each item in the list
        For answer1 As Integer = 0 To answers.Count - 1
            'pick a random answer
            answer2 = rnd.Next(0, answers.Count - 1)
            'If answer1 and answer2 are the same then skip the rest of the code
            'in the loop and go onto the next question
            If answer1 = answer2 Then
                Continue For
            End If
            'swap the questions over
            Dim temp As String
            temp = answers(answer1)
            answers(answer1) = answers(answer2)
            answers(answer2) = temp
        Next
        'return the list of answers
        Return answers
    End Function

End Class