vb.net - 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 仍在计算中。有任何想法吗?提前致谢!
解决方案
Do...While
循环只检查最后的条件。
它开始循环的迭代,然后获得测试分数。
'Prompt user for a score
strInput = InputBox("Enter test Score")
然后它执行循环体,直到它到达最后,它看到分数-1
并退出循环。
另一种写法是让循环无限期地运行。
Do
//Body
Loop While True
然后检查分数是否为-1
并退出循环。
If intTestScore = -1
Exit Do
推荐阅读
- node.js - 如何在 Apache 服务器上部署和托管 React 应用程序
- c# - OAuth 令牌请求 unsupported_grant_type
- angular - 如何创建 Android .keystore 以发布 NativeScript Angular apk
- sql-server - 为非索引列自动创建统计信息的目的是什么?
- sql - 理解 SQL 代码所需的说明
- pip - 无法从 pycharm 安装 rpy2
- maxima - 如何使“at()”函数评估 en 表达式?
- react-native - 可以像 iPad 上的 iPhone 一样只显示数字键盘吗?
- apache-spark - 将数组分解成列 Spark
- regex - 如何使用正则表达式避免基于前缀的字符串