对于循环问题 - 请帮助 [英] For Loop question - please help
问题描述
你好大师,
你们都好吗?我只是有一个问题。
我正在使用asp.net/vb.net和sql server为数据库构建这个简单的测验应用程序。现在我想要发生的是,当我点击提交按钮时,它会根据用户点击的单选按钮计算用户的所有正确答案。
我有两个单选按钮,这两个单选按钮的正确答案是A,现在如果他们点击这两个单选按钮的A,他们就会得到一个分数如果其他两个错误,它将不计算错误的答案。
这是我得到的:(我很抱歉凌乱的代码,我是新手的循环)
hello Masters,
How are you all doing? I just have a question.
I am building this simple quiz application using asp.net/vb.net and sql server for the database. Now what I want to happen is that when I click on submit button, it will count all the correct answers of the user based on the radio button he clicks.
I have two radio buttons and the correct answer for those two radio buttons is A, now if they click both A for those 2 radio buttons, they will get a score of two else if wrong, it won't count the incorrect answer.
Here is what I got: (I am very sorry for the messy code, I am new to loops)
Dim score As Integer = 0
If RbAnswers1.SelectedValue = "A" Then
For score = 0 To lbCorrectAnswerCount.Text.Count - 1
score += 1
Next score
If RbAnswers2.SelectedValue = "A" Then
Else
lbCorrectAnswerCount.Text = score
Exit Sub
End If
Else
Exit Sub
lbCorrectAnswerCount.Text = score
End If
lbCorrectAnswerCount.Text = score
提前感谢您的帮助。
Thank you in advance for your help.
推荐答案
你到那里的for循环没有多大意义。循环点是重复执行相同的任务。在您的情况下,重复任务是根据预期结果检查答案。这需要是循环的主体。尝试这样的事情:
The for loop you've got there doesn't make much sense. The point of loops is to do the same task repeatedly, In your case the repetitive task is checking the answer against the expected result. This needs to be the body of the loop. Try something like this:
Dim answers = {RdAnswer1, RdAnswer2}
Dim expected = {"A", "A"}
Dim score As Integer = 0
For i As Integer = 0 To answers.Length - 1
If answers(i).SelectedValue = expected(i) Then
score += 1
End If
Next
我们将答案和预期结果存储为数组。然后我们通过它们循环,如果我们找到匹配,我们更新分数。
We store answers and expected results as arrays. Then we loop trough them and if we find a match we update the score.
嗨Tomas Takac,
你是一个天使!非常感谢。下面是我的代码,以防初学者遇到同样的问题:
Hi Tomas Takac,
You are an angel! Thank you so much. Below is my code just in case beginners bump into the same issue as well:
Dim answers(2) As String
answers(0) = RbAnswers1.SelectedValue
answers(1) = RbAnswers2.SelectedValue
answers(2) = RbAnswers3.SelectedValue
Dim CorrectAnswer(2) As String
CorrectAnswer(0) = "A"
CorrectAnswer(1) = "A"
CorrectAnswer(2) = "A"
Dim score As Integer = 0
For i As Integer = 0 To answers.Length - 1
If answers(i) = CorrectAnswer(i) Then
score += 1
End If
Next
lbCorrectAnswerCount.Text = score
这篇关于对于循环问题 - 请帮助的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!