You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

如何让VB.NET开发的多选题程序在用户作答后切换到下一题

多选题复习程序下一题加载实现代码
Imports System.Data.OleDb

Public Class QUI
    ' 定义题目存储结构
    Private Structure QuestionInfo
        Public QuestionContent As String
        Public Answer1 As String
        Public Answer2 As String
        Public Answer3 As String
        Public RightAnswer As String ' 需在1Questions数据库表新增该字段,存储正确选项标识,如Answer1正确则存"A1"
    End Structure

    Dim ConnectString As String = ("Provider=Microsoft.ACE.OLEDB.12.0;Data Source=projectdatabase.accdb")
    Dim noofq As Integer
    Dim questionList() As QuestionInfo ' 缓存所有待作答题目
    Dim questionnum As Integer

    Private Sub QUI_Load(sender As Object, e As EventArgs) Handles MyBase.Load
        ' 获取用户输入的答题数量
        noofq = CInt(InputBox("请输入你要作答的题目数量,可选范围1-10"))
        ' 初始化题目缓存数组
        ReDim questionList(noofq - 1)
        
        ' 从数据库加载所有题目到缓存
        LoadAllQuestions()
        
        ' 加载第一题
        questionnum = 1
        LoadQuestion(questionnum)
    End Sub

    ' 加载所有题目到缓存的方法
    Private Sub LoadAllQuestions()
        Using connection As New OleDbConnection(ConnectString)
            connection.Open()
            ' 如需随机出题可将ORDER BY后的内容改为ORDER BY Rnd(ID)
            Dim cmd As New OleDbCommand("SELECT TOP " & noofq & " question, Answer1, Answer2, Answer3, RightAnswer FROM 1Questions ORDER BY ID", connection)
            Using reader As OleDbDataReader = cmd.ExecuteReader()
                Dim index As Integer = 0
                While reader.Read() And index < noofq
                    questionList(index).QuestionContent = reader("Question").ToString()
                    questionList(index).Answer1 = reader("Answer1").ToString()
                    questionList(index).Answer2 = reader("Answer2").ToString()
                    questionList(index).Answer3 = reader("Answer3").ToString()
                    questionList(index).RightAnswer = reader("RightAnswer").ToString()
                    index += 1
                End While
            End Using
        End Using
    End Sub

    ' 加载指定题号的题目到界面
    Private Sub LoadQuestion(num As Integer)
        Label1.Text = "第 " & num & " 题 / 共 " & noofq & " 题"
        ' 数组下标从0开始,题号需减1匹配
        Dim currentQ = questionList(num - 1)
        Lblquestion.Text = currentQ.QuestionContent
        RadioButtonA1.Text = currentQ.Answer1
        RadioButtonA2.Text = currentQ.Answer2
        RadioButtonA3.Text = currentQ.Answer3
        ' 清空上一题的选中状态
        RadioButtonA1.Checked = False
        RadioButtonA2.Checked = False
        RadioButtonA3.Checked = False
    End Sub

    Private Sub btnCheck_Click(sender As Object, e As EventArgs) Handles btnCheck.Click
        ' 校验用户是否选中选项
        If Not RadioButtonA1.Checked And Not RadioButtonA2.Checked And Not RadioButtonA3.Checked Then
            MsgBox("请先选择答案")
            Return
        End If

        ' 判断答案是否正确
        Dim currentQ = questionList(questionnum - 1)
        Dim userAnswer As String = ""
        If RadioButtonA1.Checked Then userAnswer = "A1"
        If RadioButtonA2.Checked Then userAnswer = "A2"
        If RadioButtonA3.Checked Then userAnswer = "A3"

        If userAnswer = currentQ.RightAnswer Then
            MsgBox("回答正确")
        Else
            MsgBox("回答错误")
        End If

        ' 处理下一题逻辑
        questionnum += 1
        If questionnum > noofq Then
            MsgBox("答题完成!")
            ' 可在此处添加重置答题或关闭窗口的逻辑
            Me.Close()
        Else
            LoadQuestion(questionnum)
        End If
    End Sub
End Class

关键修改说明

  • 新增QuestionInfo结构统一存储单题全量数据,避免零散变量维护的问题,只需提前在数据库表中新增RightAnswer字段存储正确选项标识即可使用
  • 提前批量加载指定数量的题目到内存缓存,无需每答一题都连接数据库,同时规避了DataReader仅支持单次向前读取的特性限制
  • 抽离LoadAllQuestions和LoadQuestion两个公共方法,分别负责批量拉取题目、单题渲染到界面,减少冗余代码便于后续维护
  • 修复原代码数组初始化时机错误的问题:原代码在用户未输入答题数量时就初始化了数组,会导致数组长度不符合预期引发越界错误
  • 答案判断逻辑适配所有题目,不再硬编码仅支持第一题的判断,同时增加了未选答案的校验逻辑
  • 新增答题终止判断,答完所有题目后会给出完成提示

内容的提问来源于stack exchange,提问作者Yoshi

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.27 05:15:01