我是学习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
如果您能提供帮助,我将不胜感激。谢谢。
答案 0 :(得分:0)
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 ....
,当然,如果您在-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
,则布尔值的结果为true,为什么呢? -1 is lessthan to 0
是真的。
我要做的是,我在上方插入了If statement..
和If runs < 0 then ....
,Exit Sub
以立即结束语句。如果您向-1
输入Input Text
,则结果是这样的,如果runs(-1)
小于0
,则布尔结果为true,则它将继续进行声明,即Message和Exit Sub
。
尝试上面的代码,并使用断点。.希望这对您有所帮助。