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

如何让Visual Studio Code控制台程序输入101时显示无效而非A*

解决VB.NET控制台程序输入101识别为无效的问题

问题根源

  1. 原代码里ElseIf mark >=99的条件把99、100(有效分数)和101及以上(无效分数)都归为"A*",不符合需求
  2. 直接把Console.ReadLine()赋值给Byte类型变量,输入非数字会直接报错,没做输入验证
  3. 存在拼写错误:"invailid"应该是"invalid"

修正后的代码

Imports System

Module Program
    Sub Main(args As String())
        Dim mark As Byte
        Dim grade As String

        Console.WriteLine("enter a mark out of 100")
        ' 先验证输入是否有效:是数字且分数在0-100之间
        If Not Byte.TryParse(Console.ReadLine(), mark) OrElse mark > 100 Then
            grade = "invalid"
        Else
            ' 按分数区间判断等级
            If mark <= 39 Then
                grade = "u"
            ElseIf mark <= 49 Then
                grade = "e"
            ElseIf mark <= 59 Then
                grade = "d"
            ElseIf mark <= 69 Then
                grade = "C"
            ElseIf mark <= 79 Then
                grade = "B"
            ElseIf mark <= 89 Then
                grade = "A"
            ElseIf mark <= 100 Then ' 仅90-100分评为A*
                grade = "A*"
            End If
        End If

        Console.WriteLine()
        Console.WriteLine("Grade is :" & grade)
        Console.ReadLine()
    End Sub
End Module

关键修改说明

  • 用Byte.TryParse做输入验证:既保证输入是合法数字,又能通过mark >100直接判定超分的情况为无效
  • 把A的条件限定在mark <=100,确保只有0-100范围内的高分才会被评为A,101及以上直接走无效分支
  • 修正了拼写错误,把错误的"invailid"改成正确的"invalid"

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.08.16 17:01:05