Vb.net 如何在输入框验证后启动循环?
如果为学生成绩输入了非数字值,我将尝试重新开始循环。应用程序应该有3个学生姓名和成绩输入,但当出现错误时,我的程序目前只进行下一个学生输入。我目前正在对inputbox使用并行数组方法,并尝试对输入值使用catch语句Vb.net 如何在输入框验证后启动循环?,vb.net,Vb.net,如果为学生成绩输入了非数字值,我将尝试重新开始循环。应用程序应该有3个学生姓名和成绩输入,但当出现错误时,我的程序目前只进行下一个学生输入。我目前正在对inputbox使用并行数组方法,并尝试对输入值使用catch语句 Public Class Form2 'Class level variable declaration Dim dblAverage As Double Dim studentGrade As Char Private Sub btnCalculate_Click(sender
Public Class Form2
'Class level variable declaration
Dim dblAverage As Double
Dim studentGrade As Char
Private Sub btnCalculate_Click(sender As Object, e As EventArgs) Handles btnCalculate.Click
lstOutput.Items.Clear()
Const intMAX_ROW As Integer = 2 'Highest Row subscript
Const intMAX_COL As Integer = 2 'Highest Column subscript
Dim intRow, intCol As Integer 'Row and column loop counter
Dim strName(intMAX_ROW) As String 'Name Array initilization
Dim intMarks(intMAX_ROW, intMAX_COL) As String 'Marks Array initialization
Dim intTotal As Integer
'Sum Name array through rows
For intRow = 0 To intMAX_ROW
strName(intRow) = InputBox("Enter Student name")
lstOutput.Items.Add("Test Scores for: " & strName(intRow))
'Error handling using try catch expression
Try
'Initialize accumulator
intTotal = 0
'Enter and sum Marks Column within the same row
For intCol = 0 To intMAX_COL
intMarks(intRow, intCol) = InputBox("Enter the score for test " & (intCol + 1))
If intMarks(intRow, intCol) >= 0 And intMarks(intRow, intCol) <= 100 Then
intTotal += intMarks(intRow, intCol)
Else
MessageBox.Show("Enter Marks must be between 0-100")
End If
lstOutput.Items.Add(intMarks(intRow, intCol))
Next
'To calculate average
dblAverage = intTotal / (intMAX_COL + 1)
'To Display
lstOutput.Items.Add("The total test scores for " & strName(intRow) & " is " & intTotal.ToString())
lstOutput.Items.Add("Results: ")
lstOutput.Items.Add(strName(intRow) & " Average: " & Math.Round(dblAverage) & " Grade: " & Grades())
lstOutput.Items.Add("")
Catch
MessageBox.Show("Score must be numeric between 0-100")
End Try
Next
End Sub
'Grade Condition Function
Function Grades()
If dblAverage >= 90 And dblAverage <= 100 Then
studentGrade = "A"
ElseIf dblAverage >= 80 And dblAverage <= 89 Then
studentGrade = "B"
ElseIf dblAverage >= 70 And dblAverage <= 79 Then
studentGrade = "C"
ElseIf dblAverage >= 60 And dblAverage <= 69 Then
studentGrade = "D"
Else
studentGrade = "F"
End If
Return studentGrade
End Function
Private Sub btnExit_Click(sender As Object, e As EventArgs) Handles btnExit.Click
Me.Close()
End Sub
公共类表单2
'类级别变量声明
双倍暗度
Dim studentGrade为Char
私有子btnCalculate\u单击(发送者作为对象,e作为事件参数)处理btnCalculate。单击
lstOutput.Items.Clear()
常量intMAX_行为整数=2'最高行下标
Const intMAX_COL As Integer=2'最高列下标
Dim intRow,intCol作为整数的行和列循环计数器
Dim strName(intMAX_行)作为字符串的名称数组初始化
Dim intMarks(intMAX_行,intMAX_列)作为字符串“标记数组初始化”
整数形式的整数
'通过行对名称数组求和
对于intRow=0到intMAX_行
strName(intRow)=输入框(“输入学生姓名”)
lstuput.Items.Add(“测试分数:”&strName(intRow))
'使用try-catch表达式处理错误
尝试
'初始化累加器
整数=0
'在同一行中输入和求和标记列
对于intCol=0到intMAX\U COL
intMarks(intRow,intCol)=输入框(“输入测试分数”&(intCol+1))
如果intMarks(intRow,intCol)>=0且intMarks(intRow,intCol)Try/Catch用于您无法控制的意外错误。用户输入的验证在您的控制范围内,不正确的用户输入肯定是意料之中的
您的应用程序请求一个类来存储学生的姓名和成绩信息。类的使用使代码更具可读性
Public Class StudentGrades
Public Property StudentName As String
Public Property TestScores As New List(Of Integer)
Public Property DisplayScores As String
Public Property Average As Double
Public Property Grade As String
Public Sub New(Name As String)
StudentName = Name
End Sub
Public Sub SetAverage()
'Avoid division by zero
If TestScores.Count > 0 Then
Average = TestScores.Sum / TestScores.Count
Else
Average = 0
End If
SetGrade()
End Sub
Private Sub SetGrade()
Select Case Average
Case 90 To 101
Grade = "A"
Case 80 To 90
Grade = "B"
Case 70 To 80
Grade = "C"
Case 60 To 70
Grade = "D"
Case Else
Grade = "F"
End Select
SetDisplayScores()
End Sub
Private Sub SetDisplayScores()
Dim sb As New StringBuilder
For Each i In TestScores
sb.Append(i.ToString & ",")
Next
'Trim the final comma from the string
DisplayScores = sb.ToString.Trim(","c)
End Sub
End Class
该类从属性开始StudentName
是单个字符串。由于可以有多个测试分数,我们使用列表(T)
来保存分数。表单中的用户输入设置StudentName
并填充分数列表<代码>显示分数
是由列表中的分数组成的单个字符串。DataGridView
需要这个来显示分数
接下来是类的构造函数。子新建
创建类的实例。此构造函数需要设置StudentName
属性的字符串输入
我们调用SetAverage
的形式依次调用SetGrade
调用SetDisplayScores
。因此,所有属性都已设置
在表单中,创建一个StudentGrades
列表,以保存每个学生及其所有数据。这是在表单
级别,因此表单上的所有方法都可以看到它,只要表单看到它,它就会退出
在按钮1
中,获取用户输入的姓名,验证并创建一个新的StudentGrades
,传递用户输入的姓名。接下来,我们通过调用ValidateScore
方法来循环获取用户输入以进行分数验证。验证后,分数将添加到程序前面创建的StudentGrades
实例(stu
)的分数列表中
当用户对消息框回答“否”时,调用stu
(类的当前实例)上的SetAverage
方法,该方法将设置StudentGrades
的其余属性。最后,将stu
添加到表单级别StuList
最后,在按钮3中,将DataGridView
的DataSouce
属性设置为StuList
,这样就有了用户输入的所有数据
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
Dim StuName = InputBox("Enter Student name")
'Loop until a name is entered.
Do While String.IsNullOrWhiteSpace(StuName)
StuName = InputBox("You must enter a name before recording grades")
Loop
Dim stu As New StudentGrades(StuName)
Dim MBResult As DialogResult
'Loop until user is done entering scores for this student.
Do
Dim strScore = InputBox($"Enter the score for test {stu.StudentName}")
If Not ValidateScore(strScore) Then
'Loop until user enters a valid score.
Do
strScore = InputBox("You must enter a number between 0 and 100")
Loop Until ValidateScore(strScore)
End If
stu.TestScores.Add(CInt(strScore))
MBResult = MessageBox.Show($"Add another score for {StuName}?", "Add Another?", MessageBoxButtons.YesNo)
Loop Until MBResult = DialogResult.No
stu.SetAverage()
StuList.Add(stu)
End Sub
Private Function ValidateScore(strScore As String) As Boolean
Dim score As Integer
If Integer.TryParse(strScore, score) AndAlso score >= 0 AndAlso score <= 100 Then
Return True
End If
Return False
End Function
Private Sub Button3_Click(sender As Object, e As EventArgs) Handles Button3.Click
DataGridView1.DataSource = StuList
End Sub
Private子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
Dim StuName=输入框(“输入学生姓名”)
'循环,直到输入名称。
Do While String.IsNullOrWhiteSpace(StuName)
StuName=InputBox(“记录成绩前必须输入名称”)
环
Dim stu作为新学生年级(Stuame)
Dim MBResult作为对话框结果
'循环,直到用户完成输入该学生的分数。
做
Dim strScore=InputBox($“输入测试{stu.StudentName}的分数”)
如果没有验证核心(strScore),则
'循环,直到用户输入有效分数。
做
strScore=InputBox(“您必须输入一个介于0和100之间的数字”)
循环直到ValidateScore(strScore)
如果结束
学生测试分数添加(CInt(strScore))
MBResult=MessageBox.Show($“为{StuName}添加其他分数?”,“添加其他分数?”,MessageBoxButtons.YesNo)
循环直到MBResult=DialogResult。否
stu.SetAverage()
StuList.Add(stu)
端接头
私有函数ValidateScore(strScore作为字符串)作为布尔值
将分数设置为整数
如果Integer.TryParse(strScore,score)和also score>=0,并且首先是also score,那么“重新开始循环”是什么意思?从字面上说,这意味着放弃你收到的任何输入,从头开始。这似乎效率很低。也许你的意思是重复当前的迭代,但这也是低效的。为什么不一直提示用户输入当前值,直到他们给出有效的值?这意味着将InputBox
调用放在Do…循环中,而循环。一直提醒他们,直到他们给你东西。