我需要生成随机问答。
这是aspx代码
<asp:Label ID="lblQuestion" runat="server" Text="Label"></asp:Label>
<br />
<asp:RadioButton ID="RadioButton1" runat="server" />
<br />
<asp:RadioButton ID="RadioButton2" runat="server" />
<br />
<asp:RadioButton ID="RadioButton3" runat="server" />
<br />
<asp:RadioButton ID="RadioButton4" runat="server" />
<br />
<asp:Button ID="Button1" runat="server" Text="Button" />
这是vb代码
Public Property Counter() As Integer
Get
Return IIf(ViewState("counter") Is Nothing, 1, CInt(ViewState("counter")))
End Get
Set(ByVal value As Integer)
ViewState("counter") = value
End Set
End Property
Sub question()
conn.Open()
Dim cmd As New SqlCommand("Select * From questionbank Where id=@Idquestion", conn)
cmd.Parameters.AddWithValue("@Idquestion", Counter)
Dim dr1 As SqlDataReader
dr1 = cmd.ExecuteReader
Try
While dr1.Read()
Me.lblSoalan.Text = dr1("question")
Me.RadioButton1.Text = dr1("rightanswer")
Me.RadioButton2.Text = dr1("wrong1")
Me.RadioButton3.Text = dr1("wrong2")
Me.RadioButton4.Text = dr1("wrong3")
End While
Catch ex As Exception
MsgBox("Error", MsgBoxStyle.Exclamation, "Error")
Finally
conn.Close()
End Try
End Sub
我有一套问题。用户需要单击下一步按钮才能转到另一个问题。每当用户尝试回答同一组问题时,我希望系统随机化问题
答案 0 :(得分:0)
在执行SELECT
语句之前,您需要随机化问题的ID。在随机值时替换Counter
。使用CInt(Math.Ceiling(Rnd() * n))
获取0到N(包括)
cmd.Parameters.AddWithValue("@Idquestion", CInt(Math.Ceiling(Rnd() * n)))