系统如何识别问题是否已经得到回答

qcuzuvrc  于 2021-06-18  发布在  Mysql
关注(0)|答案(1)|浏览(427)

我想知道如果用户想回到上一个问题,系统如何识别问题是否已经回答。如果答案已经被回答,答案将会更新,或者分数增加或者不被编辑,答案将会是相同的。
按钮1是返回到上一个问题按钮2是检查是真是假回答按钮3是继续考试

以下是我的代码片段:

  1. Imports MySql.Data.MySqlClient
  2. Imports System.Drawing
  3. Imports System.IO
  4. Public Class Exam
  5. 'declaring variables for connection'
  6. Dim score As Integer
  7. Dim rightans As String
  8. Dim correct As Integer = 0
  9. Dim choice As String
  10. Dim choice1 As String
  11. Dim choice2 As String
  12. Dim choice3 As String
  13. Dim con As MySqlConnection
  14. Dim con1 As MySqlConnection
  15. Dim COMMAND As MySqlCommand
  16. Dim read As MySqlDataReader
  17. Dim da As MySqlDataAdapter
  18. Dim sql As String
  19. Private Sub Exam_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  20. 'setting the radiobutton to false, so that when the form load there is no shaded button'
  21. Label2.Text = 1
  22. A.Checked = False
  23. B.Checked = False
  24. C.Checked = False
  25. D.Checked = False
  26. con = New MySqlConnection
  27. Button1.Enabled = False
  28. con.ConnectionString = "server=localhost;userid=root;password=;database=user;"
  29. 'calling sub
  30. qno()
  31. End Sub
  32. Sub clear()
  33. 'to set the radiobutton false/no shaded.
  34. A.Checked = False
  35. B.Checked = False
  36. C.Checked = False
  37. D.Checked = False
  38. End Sub
  39. Sub qno()
  40. 'calling connection'
  41. Try
  42. con = New MySqlConnection
  43. con.ConnectionString = "server=localhost; user id=root; password=; database=user;"
  44. con.Open()
  45. sql = "SELECT * FROM user.math WHERE question_id = @ID;"
  46. COMMAND = New MySqlCommand
  47. With COMMAND
  48. .Connection = con
  49. .CommandText = sql
  50. .Parameters.Clear()
  51. .Parameters.AddWithValue("@ID", Label2.Text)
  52. .ExecuteNonQuery()
  53. End With
  54. Dim arrImage() As Byte
  55. Dim dt As New DataTable
  56. da = New MySqlDataAdapter
  57. da.SelectCommand = COMMAND
  58. da.Fill(dt)
  59. If dt.Rows.Count > 0 Then
  60. arrImage = dt.Rows(0).Item(7)
  61. Dim mstream As New System.IO.MemoryStream(arrImage)
  62. Pic1.Image = Image.FromStream(mstream)
  63. question.Text = dt.Rows(0).Item(1)
  64. A.Text = dt.Rows(0).Item(2)
  65. B.Text = dt.Rows(0).Item(3)
  66. C.Text = dt.Rows(0).Item(4)
  67. D.Text = dt.Rows(0).Item(5)
  68. Else
  69. MsgBox("No results!")
  70. End If
  71. Catch ex As MySqlException
  72. MsgBox(ex.Message)
  73. Finally
  74. con.Close()
  75. da.Dispose()
  76. End Try
  77. End Sub
  78. Sub increment()
  79. 'incrementing the score f the answer is correct'
  80. Dim i As Integer = 0
  81. i = Label2.Text
  82. i = i + 1
  83. Label2.Text = i
  84. If Label2.Text > 1 Then
  85. Button1.Enabled = True
  86. End If
  87. End Sub
  88. Sub decrement()
  89. 'incrementing the score f the answer is correct'
  90. Dim i As Integer = 1
  91. i = Label2.Text
  92. i = i - 1
  93. Label2.Text = i
  94. If Label2.Text = 1 Then
  95. Button1.Enabled = False
  96. End If
  97. End Sub
  98. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  99. 'checking of the user answer the questions'
  100. If ((A.Checked = False) And (B.Checked = False) And (C.Checked = False) And (D.Checked = False)) Then
  101. MsgBox("Please answer the question")
  102. Else
  103. 'if the examinee answers all the examination it will call another questions from database'
  104. If A.Checked = True Then
  105. con.Open()
  106. ' Dim ans As String
  107. Dim arren As String = "A"
  108. Dim sql As String = ("select answer from user.math where question_id = '" & Label2.Text & "' ")
  109. COMMAND = New MySqlCommand(sql, con)
  110. Dim it As String
  111. read = COMMAND.ExecuteReader
  112. If read.HasRows Then
  113. If read.Read Then
  114. it = read.Item("answer")
  115. If it = choice Then
  116. correct = correct + 1
  117. Label4.Text = correct
  118. ElseIf it <> choice And Label2.Text <= 1 Then
  119. correct = correct - 1
  120. Label4.Text = correct
  121. End If
  122. End If
  123. clear()
  124. End If
  125. If Label2.Text = 10 Then
  126. MessageBox.Show("proceed to other subject test")
  127. End If
  128. con.Close()
  129. Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
  130. decrement()
  131. qno()
  132. End Sub
  133. End Class
u2nhd7ah

u2nhd7ah1#

因为我没有你的数据库,所以没有经过测试。上面和内联的评论。

  1. Public Class Form3
  2. Dim correct As Integer = 0
  3. Private Sub Exam_Load(sender As Object, e As EventArgs) Handles MyBase.Load
  4. 'setting the radiobutton to false, so that when the form load there is no shaded button'
  5. Label2.Text = "1"
  6. A.Checked = False
  7. B.Checked = False
  8. C.Checked = False
  9. D.Checked = False
  10. Button1.Enabled = False
  11. qno()
  12. End Sub
  13. Private Sub clear()
  14. 'to set the radiobutton false/no shaded.
  15. A.Checked = False
  16. B.Checked = False
  17. C.Checked = False
  18. D.Checked = False
  19. End Sub
  20. Private Sub qno()
  21. Try
  22. 'Using...End Using blocks will close and dispose of your objects
  23. Using con = New MySqlConnection("server=localhost;userid=root;password=;database=user;")
  24. Using Cmd = New MySqlCommand("SELECT * FROM user.math WHERE question_id = @ID;", con)
  25. Cmd.Parameters.AddWithValue("@ID", Label2.Text)
  26. Dim arrImage() As Byte
  27. Dim dt As New DataTable
  28. Using da = New MySqlDataAdapter
  29. da.SelectCommand = Cmd
  30. da.Fill(dt)
  31. If dt.Rows.Count > 0 Then
  32. 'Not at all sure about this picture code
  33. arrImage = CType(dt.Rows(0).Item(7), Byte())
  34. Dim mstream As New System.IO.MemoryStream(arrImage)
  35. Pic1.Image = Image.FromStream(mstream)
  36. question.Text = dt.Rows(0).Item(1).ToString
  37. A.Text = dt.Rows(0).Item(2).ToString
  38. B.Text = dt.Rows(0).Item(3).ToString
  39. C.Text = dt.Rows(0).Item(4).ToString
  40. D.Text = dt.Rows(0).Item(5).ToString
  41. 'guessing that answer it item 6
  42. HiddenLabel.Text = dt.Rows(0).Item(6).ToString
  43. Else
  44. MsgBox("No results!")
  45. End If
  46. End Using
  47. End Using
  48. End Using
  49. Catch ex As MySqlException
  50. MsgBox(ex.Message)
  51. End Try
  52. End Sub
  53. Private Sub Button2_Click(sender As Object, e As EventArgs) Handles Button2.Click
  54. 'checking of the user answer the questions'
  55. If ((A.Checked = False) And (B.Checked = False) And (C.Checked = False) And (D.Checked = False)) Then
  56. MsgBox("Please answer the question")
  57. Exit Sub
  58. End If
  59. 'The correct answer was put in the HiddenLabel.Text (Visible set to False)
  60. 'in the qno Sub
  61. Dim CorrectAnswer As String = HiddenLabel.Text
  62. Dim UserChoice As String = ""
  63. If A.Checked Then
  64. UserChoice = "A"
  65. ElseIf B.Checked Then
  66. UserChoice = "B"
  67. ElseIf C.Checked Then
  68. UserChoice = "C"
  69. Else
  70. UserChoice = "D"
  71. End If
  72. If UserChoice = CorrectAnswer Then
  73. correct += 1
  74. Else
  75. 'It is very hard to get a good score if it is decremented with every wrong answer
  76. 'Why not skip this and just give a zero for a wrong answer
  77. correct -= 1
  78. End If
  79. Label4.Text = correct.ToString
  80. clear()
  81. If Label2.Text = "10" Then
  82. MessageBox.Show("proceed to other subject test")
  83. Me.Hide()
  84. exam2.Show()
  85. Else
  86. 'Add code to keep track of question number in
  87. 'Add code to show the next question
  88. End If
  89. End Sub
展开查看全部

相关问题