1

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

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

介绍:

您将使用一个 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

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

谢谢。

4

2 回答 2

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
于 2013-10-19T12:52:05.937 回答
0

与您的原始代码唯一不同的是:在您的最终 Case 语句中使用“Case Else”代替“Case Is > 100”。这样,如果有人输入了数字以外的内容,它将处理错误。好工作,好运!

于 2013-10-19T15:21:32.637 回答