You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

Visual Basic中如何正确判断数值区间修复测验得分反馈异常问题

问题根因

你代码里的Or逻辑判断写法不符合VB.NET语法要求:
在VB中,lblScore11.Text = 90 Or 80 Or 70不会被解析为「值等于90、或者等于80、或者等于70」,而是会按运算优先级拆分:

  • 首先判断lblScore11.Text = 90得到一个布尔结果
  • 然后直接对数值80、70做布尔转换,非零数值会被转为True
  • 最终整个条件等价于(lblScore11.Text = 90) Or True Or True,结果永远为True,所以只要不是满分都会触发第二个分支。

修复方案

方案1:补全If条件的判断逻辑

每个Or两侧都要写完整的判断语句,同时建议先把得分转成数值类型存储,避免字符串隐式转换的潜在问题:

Private Sub btnFinalScore_Click(sender As Object, e As EventArgs) Handles btnFinalScore.Click
    Dim score As Integer = CInt(Val(lblScore10.Text))
    lblScore11.Text = score.ToString()
    If score = 100 Then '满分判断
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You have achieved full marks!")
    ElseIf score = 90 Or score = 80 Or score = 70 Then '70%-90%分判断
        txtFinalFeedback.AppendText("CONGRATULATIONS! - You only got a few questions wrong")
    ElseIf score = 60 Or score = 50 Or score = 40 Then '40%-60%分判断
        txtFinalFeedback.AppendText("You got a fair few questions wrong, remember to go over these topics and repeat the quiz later")
    ElseIf score = 30 Or score = 20 Or score = 10 Then '10%-30%分判断
        txtFinalFeedback.AppendText("You got a lot of questions wrong, remember to go over these topics and repeat the quiz later")
    Else
        lblScore11.Text = 0  '0分处理
        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

方案2:改用Select Case语句(更推荐)

多值匹配场景下用Select Case可读性和可维护性更高:

Private Sub btnFinalScore_Click(sender As Object, e As EventArgs) Handles btnFinalScore.Click
    Dim score As Integer = CInt(Val(lblScore10.Text))
    lblScore11.Text = score.ToString()
    Select Case score
        Case 100
            txtFinalFeedback.AppendText("CONGRATULATIONS! - You have achieved full marks!")
        Case 70, 80, 90
            txtFinalFeedback.AppendText("CONGRATULATIONS! - You only got a few questions wrong")
        Case 40, 50, 60
            txtFinalFeedback.AppendText("You got a fair few questions wrong, remember to go over these topics and repeat the quiz later")
        Case 10, 20, 30
            txtFinalFeedback.AppendText("You got a lot of questions wrong, remember to go over these topics and repeat the quiz later")
        Case Else
            lblScore11.Text = 0
            txtFinalFeedback.AppendText("You got all the questions wrong, make sure to go over all the topics and repeat the quiz later")
    End Select
End Sub

内容的提问来源于stack exchange,提问作者Idk

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.09.28 08:54:02