vb.net - “运行”功能的问题
问题描述
我是一名学习 Visual Basic 的新程序员。现在,我正在做一个关于垒球记分牌的项目。我已经在这个项目上工作了一段时间,我对一件事感到困惑。
我感到困惑的是,我输入了一个消息框,上面写着负数的无效输入,但它并没有从 lstScores 中删除它,即使出现了消息框,它仍然算作一局输入。
If runs < 0 Then
MessageBox.Show(VALID_MESSAGE)
这是代码:
Public Class frmSoftballScoreboard
Const VALID_MESSAGE As String = "Enter valid runs value"
Const ONLY_MESSAGE As String = "Only seven innings are allowed"
'Declaring array
Dim scores(6) As Double
'declaring variables
Dim runs As String
Dim runningScore As Integer = 0
Dim i As Integer = 0
Dim out As Double
'page load event
Private Sub Form1_Load(sender As Object, e As EventArgs) Handles MyBase.Load
lstScores.Items.Add("Runs : Running Score")
End Sub
'Enter score button
Private Sub btnScore_Click(sender As Object, e As EventArgs) Handles btnScore.Click
If i < scores.Length Then
'display inputbox to the user
runs = InputBox("Enter score for " & (i + 1) & " innings", "Score")
'if runs is entered
If runs <> "" Then
'parse the value of runs
If (Double.TryParse(runs, out)) Then
'parse the runs and add it to the array scores()
scores(i) = Double.Parse(runs)
runningScore += scores(i)
'add the rainfall value to the listbox along with month name
lstScores.Items.Add(scores(i) & " :" & runningScore)
'increment the value of i
i = i + 1
Else
'display error message
MessageBox.Show(VALID_MESSAGE)
lblTotal.Text = ""
End If
Else
'if runs is empty then display error message
MessageBox.Show("Enter runs for " & i & "innings")
End If
Else
MessageBox.Show(ONLY_MESSAGE)
End If
If runs < 0 Then
MessageBox.Show(VALID_MESSAGE)
End If
'calculate total runs And display on the lable
If scores(6) = 7 Then
lblTotal.Text = String.Format("final score is {0}", scores.Sum())
End If
End Sub
'Clear Menu click
Private Sub ClearToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles mnuClear.Click
lstScores.Items.Clear()
lblTotal.Text = ""
'reset i to 0
i = 0
End Sub
'Exit Menu click
Private Sub ExitToolStripMenuItem_Click(sender As Object, e As EventArgs) Handles mnuExit.Click
'close application
Application.Exit()
End Sub
End Class
如果您能提供帮助,我将不胜感激。谢谢你。
解决方案
Private Sub btnScore_Click(sender As Object, e As EventArgs) Handles btnScore.Click
If i < scores.Length Then
'display inputbox to the user
runs = InputBox("Enter score for " & (i + 1) & " innings", "Score")
'if runs is entered
If runs < 0 Then
MessageBox.Show(VALID_MESSAGE)
Exit Sub
ElseIf runs <> "" Then
'parse the value of runs
If (Double.TryParse(runs, out)) Then
'parse the runs and add it to the array scores()
scores(i) = Double.Parse(runs)
runningScore += scores(i)
'add the rainfall value to the listbox along with month name
lstScores.Items.Add(scores(i) & " :" & runningScore)
'increment the value of i
i = i + 1
Else
'display error message
MessageBox.Show(VALID_MESSAGE)
lblTotal.Text = ""
End If
Else
'if runs is empty then display error message
MessageBox.Show("Enter runs for " & i & "innings")
End If
Else
MessageBox.Show(ONLY_MESSAGE)
End If
If runs < 0 Then
MessageBox.Show(VALID_MESSAGE)
End If
'calculate total runs And display on the lable
If scores(6) = 7 Then
lblTotal.Text = String.Format("final score is {0}", scores.Sum())
End If
End Sub
这就是为什么如果您输入无效数据,它将添加lstScores
到. 记住代码的阅读是从上到下开始的。If statement..
If else statement..
你的第一个If statement
是这样的。If runs <> "" then ....
, 当然如果你在 Boolean 中键入-1
值Input Text
将导致 true, If -1 <> "" = true
, 那么它将继续执行以下语句
If (Double.TryParse(runs, out)) Then
'parse the runs and add it to the array scores()
scores(i) = Double.Parse(runs)
runningScore += scores(i)
'add the rainfall value to the listbox along with month name
lstScores.Items.Add(scores(i) & " :" & runningScore)
'increment the value of i
i = i + 1
这是代码行,即使值无效或是否仍在添加值lstScores
,lstScores.Items.Add(scores(i) & " :" & runningScore)
现在,在该声明之后,您将收到一条消息:
输入有效的运行值
If runs < 0 Then
MessageBox.Show(VALID_MESSAGE)
End If
该代码是您收到消息的原因。如果你输入-1
当然布尔的结果是真的,为什么?-1 is lessthan to 0
这是真的。
我所做的是,我在If statement..
上面插入,这If runs < 0 then ....
也是Exit Sub
立即结束声明。如果您输入-1
的Input Text
结果是这样的,如果runs(-1)
小于0
布尔结果为真,那么它将继续执行语句,即 Message 和Exit Sub
。
试试我上面的代码,并使用断点..希望这会有所帮助..
推荐阅读
- javascript - 在将图像发送到 S3 之前,在前端 javascript 中调整图像大小
- sql - 在oracle PLSQL中,我想比较应该以字母[AZ]开头的字符串可以包含0-9,_和允许的空格
- javascript - React - URL 中的链接是 #[object%20HTMLParagraphElement] 而不是 #id
- ios - 在 Swift 中扩展公共类以实现部分公共访问
- powershell - 使用 powershell 将新用户添加到 Active Directory 时如何包含组
- mysql - 我如何通过使用相互依赖的 3 个表数据来实现查询以显示数据?
- android - Android Studio 没有检测到我的安卓设备
- unix - 正确组中的用户无法将文件复制到“drw-rwS---”目录
- php - Yii2 RSA 加密使用模数和指数
- postgresql - 尝试使用 CTE 计算更新列