[英]Visual Basic - Multiple Choice Test
我真的是Visual Studio的新手,我也想學習進行這樣的測試,但到目前為止,我設法做到了2個問題和4個答案。 我想知道的是如何檢查是否針對兩個以上的問題檢查了好答案。 我如何使button5更改文本以執行button1的功能,所以與其一開始就沒有兩個按鈕,而只是一個按鈕發生了變化。
到目前為止,我的代碼是:
Public Class Test1
Dim question(2, 5) As String
Dim i As Integer = 2
Private Sub Test1_Load()
question(1, 0) = "2+2="
question(1, 1) = "1"
question(1, 2) = "2"
question(1, 3) = "3"
question(1, 4) = "4"
question(2, 0) = "How old are you?"
question(2, 1) = "12"
question(2, 2) = "13"
question(2, 3) = "17"
question(2, 4) = "18"
Label1.Text = question(i - 1, 0)
nr1.Text = question(i - 1, 1)
nr2.Text = question(i - 1, 2)
nr3.Text = question(i - 1, 3)
nr4.Text = question(i - 1, 4)
End Sub
Private Sub Button5_Click(sender As Object, e As EventArgs) Handles Button5.Click
Test1_Load()
Button5.Hide()
End Sub
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If i = 2 AndAlso nr4.Checked = True Then
MessageBox.Show("Good job. You have one point")
ElseIf i = 2 AndAlso nr4.Checked = False Then
MessageBox.Show("Sorry. You are wrong")
ElseIf i = 3 AndAlso nr4.Checked Then
MessageBox.Show("Good job. Another point")
End If
i = i + 1
Test1_Load()
End Sub
End Class
您可以使用變量來存儲第一個答案是否正確,例如
Dim correctAnswers = 0
然后在您的按鈕單擊方法中,
If i = 2 AndAlso nr4.Checked = True Then
correctAnswers+= 1 'increment correctAnswers by 1
MessageBox.Show("Good job. You have one point")
ElseIf i = 2 AndAlso nr4.Checked = False Then
MessageBox.Show("Sorry. You are wrong")
ElseIf i = 3 AndAlso nr4.Checked Then
correctAnswers += 1 'increment correctAnswers by 1
MessageBox.Show("Good job. Another point")
End If
i = i + 1
Test1_Load()
您還可以添加類似
If i > 3 Then 'if i > 3, then you have run out of questions
MessageBox.Show("That's the end of the test. You answered " + correctAnswers.ToString() + " questions correctly.")
Else 'if i < 3, then there are still questions to answer
'Put the rest of your code here
End If
在您的按鈕單擊方法中。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.