首页 > 解决方案 > Visual Basic Else If 语句

问题描述

我正在为商业编程入门课程做作业。它使用 Visual Basic 2017。我们必须有一个程序,使用 do while 循环和 else if 语句来跟踪学生的成绩。教授要求我们通过在输入框中输入 -1 来结束循环,但它不能用于计算。这就是我所拥有的,但它不起作用。

Do
    'Prompt user for a score
    strInput = InputBox("Enter test Score")

    If Integer.TryParse(strInput, intTestScore) Then

        If intTestScore >= 0 And intTestScore <= 100 Then

            'Calculate running totals for each letter grade
            If intTestScore >= 93 Then
                'Increase student Count A by  1
                intStudentCountA += 1
                'add intTestScore to current score total A
                intTotalA += intTestScore

            ElseIf intTestScore >= 90 Then
                'Increase student Count A- by 1
                intStudentCountAMinus += 1
                'add intTestScore to current score total A-
                intTotalAMinus += intTestScore

            ElseIf intTestScore >= 87 Then
                'Increase student Count B+ by 1
                intStudentCountBPlus += 1
                'add intTestScore to current score total B+
                intTotalBPlus += intTestScore

            ElseIf intTestScore >= 83 Then
                'Increase student Count B by 1
                intStudentCountB += 1
                'add intTestScore to current score total B
                intTotalB += intTestScore

            ElseIf intTestScore >= 80 Then
                'Increase student Count B- by 1
                intStudentCountBMinus += 1
                'add intTestScore to current score total B-
                intTotalBMinus += intTestScore

            ElseIf intTestScore >= 77 Then
                'Increase student Count C+ by 1
                intStudentCountCPlus += 1
                'add intTestScore to current score total C+
                intTotalCPlus += intTestScore

            ElseIf intTestScore >= 73 Then
                'Increase student Count C by 1
                intStudentCountC += 1
                'add intTestScore to current score total C
                intTotalC += intTestScore

            ElseIf intTestScore >= 70 Then
                'Increase student Count C- by 1
                intStudentCountCMinus += 1
                'add intTestScore to current score total C-
                intTotalCMinus += intTestScore

            ElseIf intTestScore >= 67 Then
                'Increase student Count D+ by 1
                intStudentCountDPlus += 1
                'add intTestScore to current score total D+
                intTotalDPlus += intTestScore

            ElseIf intTestScore >= 63 Then
                'Increase student Count D by 1
                intStudentCountD += 1
                'add intTestScore to current score total D
                intTotalD += intTestScore

            ElseIf intTestScore >= 60 Then
                'Increase student Count D- by 1
                intStudentCountDMinus += 1
                'add intTestScore to current score total D-
                intTotalDMinus += intTestScore

            ElseIf intTestScore >= 0 Then
                'Increase student Count F by 1
                intStudentCountF += 1
                'add intTestScore to current score total F
                intTotalF += intTestScore

            End If

        End If
        'running total
        intTotal += intTestScore
        'increase student counter by 1
        intStudentCount += 1


        'add the score to listbox
        lstScore.Items.Add(intTestScore.ToString())

    Else
        MessageBox.Show("The value must be an integer. The maximum possible score is 100.")

    End If

Loop While intTestScore <> -1

我没有包括变量,因为这篇文章真的很长。我不确定为什么 -1 仍在计算中。有任何想法吗?提前致谢!

标签: vb.netif-statement

解决方案


Do...While循环只检查最后的条件。

它开始循环的迭代,然后获得测试分数。

'Prompt user for a score
strInput = InputBox("Enter test Score")

然后它执行循环体,直到它到达最后,它看到分数-1并退出循环。

另一种写法是让循环无限期地运行。

Do
    //Body
Loop While True

然后检查分数是否为-1并退出循环。

If intTestScore = -1
    Exit Do

推荐阅读