2014-04-16 45 views
0

我需要生成隨機問題和答案。在vb.net中隨機化mcq?

這裏的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 [email protected]", 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

在執行SELECT語句之前,您需要隨機化問題的ID。當一個隨機值替換您的Counter。使用CInt(Math.Ceiling(Rnd() * n)) 0和N(含)之間,以獲得一個隨機整數值

cmd.Parameters.AddWithValue("@Idquestion", CInt(Math.Ceiling(Rnd() * n))) 
+0

它正在然而問題是,問題是不斷隨機eventhough我沒有點擊下一步按鈕,如何確保回答的問題當用戶點擊下一個按鈕時不會重複? – luly

+0

基本上有兩種方式來擺脫重複: ** ** 1 [**創建一個列表,從該列表中隨機做出選擇,當你選擇它刪除每個項目**](HTTP:// 。www.vbforums.com/showthread.php?t=393023) ** 2 ** [**創建列表,隨機化,然後從第一選擇從它的項目持續**](HTTP:// www.vbforums.com/showthread.php?t=585813) – equisde

+0

這只是隨機的問題。答案如何? – luly