【问题标题】:Visual Basic Selection using a case statement task check使用 case 语句任务检查的 Visual Basic 选择
【发布时间】:2013-10-19 12:10:59
【问题描述】:

只是想知道我为 Visual Basic 计算作业所做的是否正确。我们的活动任务:

“活动:使用案例陈述选择成绩

简介:

您将使用 case 语句,允许用户以整数值形式输入成绩值,并将成绩作为从 A 到 E 的字母返回。

选择逻辑

  • 如果输入的数字在 91-100 之间,则输出将为 A

  • 如果输入的数字介于 81 和 90 之间,则输出将为 B

  • 如果输入的数字介于 71 和 80 之间,则输出将为 C

  • 如果输入的数字介于 61 和 70 之间,则输出将为 D

  • 如果输入的数字介于 51 和 60 之间,则输出将为 E

    任何低于 50 的都是 失败
    任何高于 100 的值都是不正确的值,他们将不得不再次运行程序。
    创建必要的变量
    创建必要的输出,告诉用户程序的目的 创建代码以读取用户的名字
    创建将成绩读取为整数值的代码
    根据上述标准创建产生相关成绩的代码。
    创建必要的输出代码以将用户的名字和他们的等级输出为字母“

我的代码:

Module Module1

Sub Main()
    Dim anum As Integer
    Dim name As String
    Console.WriteLine("This programme converts marks into grades")
    Console.WriteLine("Please enter name...")
    name = Console.ReadLine
    Console.WriteLine("Please enter number of marks...")
    anum = Console.ReadLine()
    Select Case anum
        Case 91 To 100
            Console.WriteLine(name & " receives an A.")
        Case 81 To 90
            Console.WriteLine(name & " receives a B.")
        Case 71 To 80
            Console.WriteLine(name & " receives a C.")
        Case 61 To 70
            Console.WriteLine(name & " receives a D.")
        Case 51 To 60
            Console.WriteLine(name & " receives an E.")
        Case Is <= 50
            Console.WriteLine(name & ", unfortunately failed.")
        Case Is > 100
            Console.WriteLine(name & ", this is an incorrect value. Please try again.")
    End Select
End Sub

End Module

如果有人能确认它是正确的或告诉我我是否做错了什么或需要添加一些东西,我将不胜感激!

谢谢。

【问题讨论】:

    标签: vb.net visual-studio numbers range console-application


    【解决方案1】:

    原始代码似乎还可以,只是我使用正确的数据类型对其进行了改进,添加了基本的异常转换并尝试简化事情:

    Module Module1
    
    ' Store ranges and chars
    ReadOnly TupleList As New List(Of Tuple(Of Short, Short, Char)) From { _
             Tuple.Create(51S, 60S, "E"c), _
             Tuple.Create(61S, 70S, "D"c), _
             Tuple.Create(71S, 80S, "C"c), _
             Tuple.Create(81S, 90S, "B"c), _
             Tuple.Create(91S, 100S, "A"c) _
    }
    
    ' Set custom strings formatting
    ReadOnly str_OK As String = "{0}, receives an {1}."
    ReadOnly str_FAIL As String = "{0}, unfortunately failed."
    ReadOnly str_INCORRECT As String = "{0}, this is an incorrect value. Please try again."
    
    Sub Main()
    
        ' Initialize user variables with a default value (0)
        Dim anum As Short = 0
        Dim name As String = 0
    
        Console.WriteLine("This programme converts marks into grades")
        Console.WriteLine("Please enter name...")
        name = CStr(Console.ReadLine)
    
        Try
            Console.WriteLine("Please enter number of marks...")
            anum = CShort(Console.ReadLine())
    
        Catch ex As FormatException
            Console.WriteLine("Please enter a valid number...")
            Environment.Exit(1) ' Exit from application returning an error exitcode
    
        Catch ex As Exception
            Console.WriteLine(String.Format("{0}: {1}", _
                                            ex.Message, _
                                            ex.StackTrace))
            Environment.Exit(1) ' Exit from application returning an error exitcode
    
        End Try
    
        Select Case anum
    
            Case Is <= 50
                Console.WriteLine(String.Format(str_FAIL, name))
    
            Case Is >= 101
                Console.WriteLine(String.Format(str_INCORRECT, name))
    
            Case Else ' User value is inside an accepted range
                For Each Item As Tuple(Of Short, Short, Char) In TupleList
                    If (anum >= Item.Item1 AndAlso anum <= Item.Item2) Then
                        Console.WriteLine(String.Format(str_OK, name, Item.Item3))
                        Environment.Exit(0) ' Exit from application
                        ' Exit For
                    End If
                Next Item
    
        End Select
    
        Environment.Exit(1) ' Exit from application returning an error exitcode
        ' ( When Is <= 50 or Is >= 101 )
    
    End Sub
    
    End Module
    

    【讨论】:

    • 哦,哇,感谢这位伙伴,但我绝对没有在 vb 中达到这么复杂的阶段。还是谢谢!
    【解决方案2】:

    与您的原始代码相比,我唯一会做的事情是: 使用“Case Else”代替“Case Is > 100”作为您的最终案例陈述。 这样,如果有人输入了数字以外的内容,它将处理错误。 祝你工作顺利,祝你好运!

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2023-03-22
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多