如何获取一系列值 - Visual basic

How to get a range of values - Visual basic

我做了一个测验,在测验结束时,用户会根据他们的成绩得到反馈。 这是代码:

Private Sub btnFinalScore_Click(sender As Object, e As EventArgs) Handles btnFinalScore.Click
    lblScore11.Text = Val(lblScore10.Text)
    If lblScore11.Text = 100 Then 'Deals if the user gets full marks
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You have achieved full marks!")
    ElseIf lblScore11.Text = 90 Or 80 Or 70 Then 'Deals if the user gets between 70% to 90%l marks
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You only got a few questions wrong")
    ElseIf lblScore11.Text = 60 Or 50 Or 40 Then 'Deals if the user gets between 40% to 60%l marks
        txtFinalFeedback.AppendText("You got a fair few questions wrong, remember to go over these topics and repeat the quiz later")
    ElseIf lblScore11.Text = 30 Or 20 Or 10 Then 'Deals if the user gets between 10% to 30%l marks
        txtFinalFeedback.AppendText("You got a a lot of questions wrong, remember to go over these topics and repeat the quiz later")
    Else
        lblScore11.Text = 0  'Deals if the user gets no marks
        txtFinalFeedback.AppendText("You got all the questions wrong, make sure to go over all the topics and repeat the quiz later")
    End If
End Sub

如果用户得到 100 分,则第一行代码可以正常工作,但如果用户有任何错误,则始终会给出第二个反馈。我该如何解决这个问题?

您应该将分数作为数值而不是文本来处理。

那么你只需要检查分数是否大于或等于每个括号中的最低分数:

Private Sub btnFinalScore_Click(sender As Object, e As EventArgs) Handles btnFinalScore.Click
    lblScore11.Text = lblScore10.Text
    Dim score As Integer = CInt(lblScore10.Text)
    If score = 100 Then 'Deals if the user gets full marks
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You have achieved full marks!")
    ElseIf score >= 70 Then 'Deals if the user gets between 70% to 90%l marks
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You only got a few questions wrong")
    ElseIf score >= 40 Then 'Deals if the user gets between 40% to 60%l marks
        txtFinalFeedback.AppendText("You got a fair few questions wrong, remember to go over these topics and repeat the quiz later")
    ElseIf score >= 10 Then 'Deals if the user gets between 10% to 30%l marks
        txtFinalFeedback.AppendText("You got a a lot of questions wrong, remember to go over these topics and repeat the quiz later")
    Else
        txtFinalFeedback.AppendText("You got all the questions wrong, make sure to go over all the topics and repeat the quiz later")
    End If
End Sub

你应该设置Option Strict On。参见:What do Option Strict and Option Explicit do?。如果不这样做,您将不会在进行不需要的转换时收到错误消息。

你写ElseIf lblScore11.Text = 90 Or 80 Or 70 Then。这并不符合您的预期,因为您必须编写 ElseIf lblScore11.Text = 90 Or lblScore11.Text = 80 Or lblScore11.Text = 70 Then。它现在所做的是将 80 和 90 转换为 True,因为那里需要一个布尔值。所以,它确实 ElseIf (lblScore11.Text = 90) Or True Or True Then,而且总是 True。或者它是否与 80 Or 70 进行按位或并将其转换为 True 以将其与第一个结果进行比较。我不知道。但无论如何都是错误的。

此外,如果其他值如54人参与?你只是无视他们。

您还在比较字符串和整数。将字符串转换为整数,然后进行比较。请参阅@Idle_Mind 的答案以获得好的解决方案。

您应该检查您的命名约定。 lblScore11、lblScore10 是糟糕的名字,很容易引起混淆和错误。 txtFinalFeedback 有意义。

而且这段代码应该更直接。为什么不直接取lblScore10的值呢

对于这种类型的构造,我认为使用 select case 语句更为优雅,如果您将使用数字范围,它也可能很有用。因此,代码可以这样重构:

Select Case score
    Case 100 'Deals if the user gets full marks
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You have achieved full marks!")
    Case Is >= 70 'Deals if the user gets between 70% to 90%l marks
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You only got a few questions wrong")
    Case Is >= 40  'Deals if the user gets between 40% to 60%l marks
        txtFinalFeedback.AppendText("You got a fair few questions wrong, remember to go over these topics and repeat the quiz later")
    Case Is >= 10  'Deals if the user gets between 10% to 30%l marks
        txtFinalFeedback.AppendText("You got a a lot of questions wrong, remember to go over these topics and repeat the quiz later")
    Case Else
        txtFinalFeedback.AppendText("You got all the questions wrong, make sure to go over all the topics and repeat the quiz later")
End Select