我需要制作一个程序,找到15个测试等级的平均值

时间:2015-12-17 22:12:39

标签: vb.net exception exception-handling comments average

首先,程序必须找到15个数字的平均值。然后必须给出相应的分数例如,如果程序打印等级B的等级,那么程序必须使用某种用户定义的例子。

现在提出我的问题。在下面的程序中,我得到它来计算15个数字的平均值,但是,我无法找到一种方法来检测用户是否输入了除数字之外的其他内容。我试图让它尝试将gradetotal作为十进制,但这似乎是不可接受的。那么有另一种方式还是我忽略了什么?

Module Module1

    Sub Main()
        Dim num1, num2, num3, num4, num5, num6, num7, num8, num9, num10, num11, num12, num13, num14, num15, gradetotal As Decimal
        Console.WriteLine("The Average Of 10 numbers")
        Console.Write("Input Test Grade 1: ")
        num1 = Console.ReadLine()
        Console.Write("Input Test Grade 2: ")
        num2 = Console.ReadLine()
        Console.Write("Input Test Grade 3: ")
        num3 = Console.ReadLine()
        Console.Write("Input Test Grade 4: ")
        num4 = Console.ReadLine()
        Console.Write("Input Test Grade 5: ")
        num5 = Console.ReadLine()
        Console.Write("Input Test Grade 6: ")
        num6 = Console.ReadLine()
        Console.Write("Input Test Grade 7: ")
        num7 = Console.ReadLine()
        Console.Write("Input Test Grade 8: ")
        num8 = Console.ReadLine()
        Console.Write("Input Test Grade 9: ")
        num9 = Console.ReadLine()
        Console.Write("Input Test Grade 10: ")
        num10 = Console.ReadLine()
        Console.Write("Input Test Grade 11: ")
        num11 = Console.ReadLine()
        Console.Write("Input Test Grade 12: ")
        num12 = Console.ReadLine()
        Console.Write("Input Test Grade 13: ")
        num13 = Console.ReadLine()
        Console.Write("Input Test Grade 14: ")
        num14 = Console.ReadLine()
        Console.Write("Input Test Grade 15: ")
        num15 = Console.ReadLine()

        gradetotal = num1 + num2 + num3 + num4 + num5 + num6 + num7 + num8 + num9 + num10 + num11 + num12 + num13 + num14 + num15

        Try
            If gradetotal = Decimal Then

            Else
                Throw New Exception()
            End If
        Catch ex As Exception
            Console.WriteLine("Your Grade Average Is:" & (gradetotal) / 15)

        End Try
        Console.ReadLine()

    End Sub
End Module

最后有人可以向我解释如何让这个程序有一个评论块,其中包含所有关键信息以及单行和多行评论吗?

1 个答案:

答案 0 :(得分:1)

要检查您的用户是否编写了数字而没有引发异常,您应该使用Decimal.TryParse方法,如果键入的值不是十进制数,则返回false。当然,十进制数字很棘手,因为您还应该考虑文化中使用的字符来指示小数分隔符。

说,您还需要使用循环更改该大输入序列并存储在某种集合中接收的输入。在下面的示例中,我使用List(Of Decimal)并应用IEnumerable扩展来获取输入的总和和平均值。

Sub Main
    Dim count As Integer = 0
    Dim grades = New List(Of Decimal)()
    While count < 15
        Console.WriteLine("Enter " & count + 1 & " grade")
        Dim input = Console.ReadLine()
        Dim temp As Decimal
        ' InvariantCulture uses the point as decimal separator,
        ' if your culture uses something different use CurrentCulture instead
        If Decimal.TryParse(input, System.Globalization.NumberStyles.AllowDecimalPoint,
                                   System.Globalization.CultureInfo.InvariantCulture, temp) Then
            grades.Add(temp)
            count += 1
        End If 
    End While

    Console.WriteLine("Total:" & grades.Sum())
    Console.WriteLine("Average:" & grades.Average())
End Sub