如何根据问题生成随机问题 [英] ho to generate Random questions according to no of questions

查看:89
本文介绍了如何根据问题生成随机问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

你好,我有一个问题,根据问题的数量生成随机问题实际上我想要randome问题,首先采取的问题就像(5,10,15,20,25 ...)并在此基础上显示随机问题...

在我的表格中我使用四个组合框,1个DataGridview和1个按钮控件... ComBoBox1用于模块,ComboBox2用于主题,ComboBox3用于级别,ComboBox4用于No问题...

当我选择模块然后向ComboBox2显示所选模块的主题然后我选择级别然后根据级别显示随机问题这是有效但我想要这个工作但是毫无疑问意味着当我们点击数字然后根据数字,级别,模块和主题显示randome问题。

我的代码是:



hello I have a Problem to generate random questions according to Number of Questions actually i want randome question which first take the no of questions just like (5,10,15,20,25...) and on this basis show random questions ...
In my Form i am using Four ComboBoxes , 1 DataGridview and 1 Button Control ... ComBoBox1 is for Modules ,ComboBox2 is for Topics, ComboBox3 is for Levels and ComboBox4 is for No of Questions...
and when i select the module then shows topic of selected module to ComboBox2 and then i select the level then shows random question according to level this is working but i want that this work but with no of question means when we click on numbers then shows randome question according to numbers,levels,Module and topic.
My Code Is:

Public Class Form1
    Dim cn As New SqlConnection("Data Source=NIDA-PC\SQLEXPRESS;Initial Catalog=Finaldb;Integrated Security=True")
    Dim da As New SqlDataAdapter
    Dim ds As New DataSet
    Dim qry As String
    Dim dt As New DataTable
    Dim dt1 As New DataTable
    Dim qry1 As String
    Dim qry3 As String
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        cn.Close()
        cn.Open()
        qry = "Select Module_ID,Module_Name from Module_tbl"
        da = New SqlDataAdapter(qry, cn)
        ds = New DataSet()
        da.Fill(ds, "tab")
        ComboBox1.DataSource = ds.Tables("tab")
        ComboBox1.DisplayMember = "Module_Name"
        ComboBox1.ValueMember = "Module_ID"
        ComboBox1.Text = ""
        cn.Close()

        cn.Close()
        cn.Open()
        qry1 = "Select Topic_ID,Topic_Name from topic_tbl"
        da = New SqlDataAdapter(qry1, cn)
        ds = New DataSet()
        da.Fill(ds, "tab")
        ComboBox2.DataSource = ds.Tables("tab")
        ComboBox2.DisplayMember = "Topic_Name"
        ComboBox2.ValueMember = "Topic_ID"
        ComboBox2.Text = ""
        cn.Close()

        ComboBox3.Items.Add("1")
        ComboBox3.Items.Add("2")
        ComboBox3.Items.Add("3")

        ComboBox4.Items.Add("5")
        ComboBox4.Items.Add("15")
        ComboBox4.Items.Add("20")
        ComboBox4.Items.Add("25")
        ComboBox4.Items.Add("30")
        ComboBox4.Items.Add("35")
        ComboBox4.Items.Add("40")
        ComboBox4.Items.Add("45")
        ComboBox4.Items.Add("50")
        ComboBox4.Items.Add("55")
        ComboBox4.Items.Add("60")
        ComboBox4.Items.Add("65")
        ComboBox4.Items.Add("70")
        ComboBox4.Items.Add("75")
        ComboBox4.Items.Add("80")
        ComboBox4.Items.Add("85")
        ComboBox4.Items.Add("90")
        ComboBox4.Items.Add("95")
        ComboBox4.Items.Add("100")

        Me.WindowState = FormWindowState.Maximized

    End Sub
  Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
        qry3 = "SELECT Question,Image_Data,Level_ID FROM Question_tbl where Level_ID= '" & ComboBox3.SelectedItem & "'And Topic_ID= '" & ComboBox2.SelectedValue.ToString & "' ORDER BY NEWID()"
        'qry3 = "SELECT Level_ID * From Question_tbl Order By NewID() "
        da = New SqlDataAdapter(qry3, cn)
        dt1.Clear()
        da.Fill(dt1)
        ComboBox2.Focus()
        ComboBox3.Focus()
        DataGridView1.DataSource = dt1
        cn.Close()
    End Sub
    Dim qry6 As String
    Dim Module_ID As Integer
    Private Sub ComboBox1_SelectedIndexChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles ComboBox1.SelectedIndexChanged
        cn.Close()
        cn.Open()
        Try
            Module_ID = Convert.ToInt32(ComboBox1.SelectedValue.ToString)
        Catch ex As Exception
            'MsgBox("Error Number")
            Return
        End Try
        'Exception handling
        'MsgBox(ComboBox1alue.ToString)
        cn.Close()

        cn.Close()
        cn.Open()
        qry6 = "Select Topic_ID,Topic_Name from Topic_tbl where Module_ID = '" & Module_ID & "'"
        da = New SqlDataAdapter(qry6, cn)
        dt.Clear()
        da.Fill(dt)
        ComboBox1.Focus()
        ComboBox2.DataSource = dt
        cn.Close()
    End Sub
End Class

推荐答案

这是我的建议方法:

首先,数据库中的每个问题都应该有一个唯一的密钥用于识别目的,我们称之为question_id,

假设你的数据库中有100个问题question_id值从1到100不等。

现在,用户想要选择10个问题,然后在代码中输入
1.生成10个不同的随机整数1到100.

2.触发一个sql来选择那10个问题从你的数据库中,question_ids等于10个随机整数。

3.用10个问题做点什么。
This is my suggested approach:
First of all, each question in the database should have an unique key for identification purpose, let call it question_id,
Assuming there are 100 questions in your database with question_id values ranging from 1 to 100.
Now, say user wants to select 10 questions, then
1. in the code, generate 10 distinct random integers in the range of 1 to 100.
2. fire an sql to select those 10 questions from your database that have question_ids equal to the 10 random integers.
3. Do something with the 10 questions.


这篇关于如何根据问题生成随机问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆