使用While或Until循环的等级平均值

时间:2013-12-14 23:27:29

标签: vba visual-studio-2012 console-application

我正在参加一个在线Visual Basic编程1课程,只需很少的指导。我花了几个小时完成每项任务,通常可以通过查找和测试来寻求帮助。我一直无法找到帮助(或者我用另一种语言找到它,我不知道如何翻译成VB控制台)。我要求用户输入测试分数。我的问题是我不知道他们会输入多少分数。我需要这个数字,以便我可以计算平均值并返回一个等级。

Module Module1

    Sub Main()
        Dim testScore As Integer = 1
        Dim scoreSum As Integer = 0
        Dim numberOfScore As Integer
        Dim average As Integer

        Console.WriteLine("Enter all the test scores. When you are done enter 0. ")

        Do While (testScore <> 0)
            testScore = Console.ReadLine()
            scoreSum = scoreSum + testScore
            numberOfScore += testScore
            average = scoreSum / numberOfScore
            Console.Write(scoreSum.ToString)
        Loop

        Console.WriteLine("The average is " + (scoreSum / numberOfScore).ToString())

        If (average >= 90) Then
            Console.WriteLine("Your Grade is A ")

        ElseIf (average >= 80) Then
            Console.WriteLine("Your Grade is B ")

        ElseIf (average >= 70) Then
            Console.WriteLine("Your Grade is C ")

        ElseIf (average >= 60) Then
            Console.WriteLine("Your Grade is D ")

        Else
            Console.WriteLine("Your Grade is F. " & "You will have to repeat this course. ")

        End If

        Console.ReadLine()


    End Sub

End Module

2 个答案:

答案 0 :(得分:0)

您需要将numberOfScore增加1而不是testScore。平均值是得分数的总和。

答案 1 :(得分:0)

Sub Main()
Dim testScore As Integer = 1
Dim scoreSum As Integer = 0
Dim numberOfScore As Integer
Dim average As Integer

Console.WriteLine("Enter all the test scores. When you are done enter 0. ")

testScore = Console.ReadLine()

Do 
    scoreSum = scoreSum + testScore
    numberOfScore += 1
    average = scoreSum / numberOfScore
    Console.Write(scoreSum.ToString)
    testScore = Console.ReadLine()
Loop While (testScore <> 0)

Console.WriteLine("The average is " + (scoreSum / numberOfScore).ToString())

If (average >= 90) Then
    Console.WriteLine("Your Grade is A ")

ElseIf (average >= 80) Then
    Console.WriteLine("Your Grade is B ")

ElseIf (average >= 70) Then
    Console.WriteLine("Your Grade is C ")

ElseIf (average >= 60) Then
    Console.WriteLine("Your Grade is D ")

Else
    Console.WriteLine("Your Grade is F. " & "You will have to repeat this course. ")

End If

Console.ReadLine()


End Sub