使用case语句任务检查的Visual Basic选择

时间:2013-10-19 12:11:00

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

只是想知道我为我的视觉基础计算功课做了什么是正确的。我们的活动任务:

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

简介:

您将使用一个案例陈述,允许用户将等级值作为整数值输入,并将等级返回为从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

如果有人能够确认它是正确的,或者告诉我是否做错了什么或者需要添加某些东西,那么会感激不尽!

感谢。

2 个答案:

答案 0 :(得分: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

答案 1 :(得分:0)

我唯一能做的就是你的原始代码: 使用“Case Else”代替“Case Is&gt; 100”作为最终的Case语句。 这样,如果有人输入的不是数字,它会处理错误。 干得好,祝你好运!