Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/vb.net/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Vb.net 多项选择题测验:分数加起来不正确_Vb.net - Fatal编程技术网

Vb.net 多项选择题测验:分数加起来不正确

Vb.net 多项选择题测验:分数加起来不正确,vb.net,Vb.net,测验结束后,我回去测试,发现分数加起来不正确。我已经检查了好几次代码,它看起来很好,但我不明白为什么它不能正确地将分数相加。我已经创建了一个子例程来检查正确的答案并标记它,然后在单击按钮时调用它 Dim NoQ = ArQOP.NoQ Dim n As Integer = 1 Dim rand As New Random Dim dr As OleDbDataReader Dim CorrectAnswer As String Public Score As Int

测验结束后,我回去测试,发现分数加起来不正确。我已经检查了好几次代码,它看起来很好,但我不明白为什么它不能正确地将分数相加。我已经创建了一个子例程来检查正确的答案并标记它,然后在单击按钮时调用它

Dim NoQ = ArQOP.NoQ
   Dim n As Integer = 1
   Dim rand As New Random
   Dim dr As OleDbDataReader
   Dim CorrectAnswer As String
   Public Score As Integer = 0
   Dim SelectedAnswer As String
   Dim username = Entry.user
   Dim Percentage As Integer
   Dim Grade As String
   Dim sqlString As String

Private Sub AnsCheck()

       Using cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=login.accdb"),
               cmd As New OleDbCommand("SELECT CorrectAns FROM MCQ WHERE QuestionNumber = '" & n & "'", cn)
           cn.Open()
           CorrectAnswer = cmd.ExecuteScalar
       End Using

       If RadioButton1.Checked Then
           SelectedAnswer = RadioButton1.Text
       End If

       If RadioButton2.Checked Then
           SelectedAnswer = RadioButton2.Text
       End If

       If RadioButton3.Checked Then
           SelectedAnswer = RadioButton3.Text
       End If

       If RadioButton4.Checked Then
           SelectedAnswer = RadioButton4.Text
       End If

       If CorrectAnswer = SelectedAnswer Then
           Score += 1
       Else
           Score = Score
       End If

   End Sub


Private Sub NxtQues_Click(sender As Object, e As EventArgs) Handles NxtQues.Click
       n += 1
       If n <= NoQ Then
           Questions()
           Answers()
           AnsCheck()
       Else
           SkipQues.Hide()
           NxtQues.Hide()
           EndQuiz.Show()
           StatsCalc()
       End If
   End Sub 

Dim NoQ=ArQOP.NoQ
尺寸n为整数=1
Dim rand作为新的随机变量
将dr设置为OLEDB数据读取器
答案为字符串
公共分数为整数=0
Dim SelectedAnswer作为字符串
Dim username=Entry.user
将百分比设置为整数
将等级设置为字符串
将sqlString设置为字符串
私人分包检查()
使用cn作为新的OLEDB连接(“Provider=Microsoft.ACE.OLEDB.12.0;数据源=login.accdb”),
cmd作为新的OleDbCommand(“从MCQ中选择CorrectAns,其中QuestionNumber=”&n&“”,cn)
cn.Open()
CorrectAnswer=cmd.ExecuteScalar
终端使用
如果RadioButton1.已检查,则
SelectedAnswer=RadioButton1.文本
如果结束
如果RadioButton2.已选中,则
SelectedAnswer=RadioButton2.文本
如果结束
如果已选中RadioButton3,则
SelectedAnswer=RadioButton3.文本
如果结束
如果已选中RadioButton4,则
SelectedAnswer=RadioButton4.文本
如果结束
如果CorrectAnswer=SelectedAnswer,则
分数+=1
其他的
分数=分数
如果结束
端接头
私有子NxtQues\u单击(发送者作为对象,e作为事件参数)处理NxtQues。单击
n+=1

如果n您可能在
中遇到类型不匹配的情况,请从MCQ中选择CorrectAns,其中QuestionNumber='&n&''“
您已将n声明为整数,但正在尝试将其连接为字符串。使用参数可以避免这些问题。如果您检查数据库以获取
OleDbType
,您将立即看到要分配给
.Value
的变量类型


cmd.ExecuteScalar
返回一个对象,因此需要将其更改为字符串

我有一个小功能来获取选中的单选按钮。在本例中,您传递的是表单的
Me
。很多时候,容器控件可能是一个GroupBox,您会传递GroupBox

我并不认为你的代码增加分数有什么问题

Private Sub AnsCheck()

    Using cn As New OleDbConnection("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=login.accdb"),
           cmd As New OleDbCommand("SELECT CorrectAns FROM MCQ WHERE QuestionNumber = @n;", cn)
        cmd.Parameters.Add("@n", OleDbType.Numeric).Value = n
        cn.Open()
        CorrectAnswer = CStr(cmd.ExecuteScalar)
    End Using

    SelectedAnswer = GetSelectedRadioButton(Me).Text

    If CorrectAnswer = SelectedAnswer Then
        Score += 1
    End If

End Sub

Private Function GetSelectedRadioButton(ctrl As ContainerControl) As RadioButton
    Dim rButton As RadioButton = ctrl.Controls.OfType(Of RadioButton).FirstOrDefault(Function(r) r.Checked = True)
    Return rButton
End Function

请打开选项“严格”。这是一个由两部分组成的过程。首先,对于当前项目-在解决方案资源管理器中双击“我的项目”。选择左边的Compile。在“选项严格”下拉列表中,选择“启用”。第二,对于未来的项目-进入工具菜单->选项->项目和解决方案->VB默认值。在“选项严格”下拉列表中,选择“启用”。这将使您在运行时避免出现bug。

您调试过这段代码吗?
cmd.ExecuteScalar()
是否返回值或
null
?为什么不在加载问题时也加载答案?您可以在RadioButton的
CheckedChanged
事件中设置当前选择的选项。(不相关)
Score=Score
不是很有用,您可以删除它。
cmd.ExecuteScalar()
确实返回一个值。老实说,这是我能想到的唯一一种标记方法,我已经启用了Option Strict,但是当我这样做时,我无法全局使用变量,我也尝试了您发送的代码,但似乎不起作用。
username
将需要一个数据类型。当你说“全球”时,你的意思是在表格内吗?在表单级别
Dim
表示
Private
,应在整个表单中提供。我的代码在哪里失败了?我是说在整个程序中。如中所示,当我将一个变量从另一个形式调用到此形式时,它会给出一个错误。而且,它并不是真的不及格,只是分数加起来不正确。我只是不明白哪里出了问题。如果从另一个类(表单)调用表单级变量,它必须是friend的公共变量。我所做的是:
public username As String
在一个表单中,然后
Dim username=Form2。username
但当我打开选项Strict时,它已停止工作