首页 > 解决方案 > 如何获得一系列值 - 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,第一行代码可以正常工作,但如果用户有任何错误,总是会给出第二个反馈。我该如何解决?

标签: vb.net

解决方案


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

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

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

推荐阅读