首页 > 解决方案 > “运行”功能的问题

问题描述

我是一名学习 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

如果您能提供帮助,我将不胜感激。谢谢你。

标签: vb.net

解决方案


 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 中键入-1Input 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

这是代码行,即使值无效或是否仍在添加值lstScoreslstScores.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立即结束声明。如果您输入-1Input Text结果是这样的,如果runs(-1)小于0布尔结果为真,那么它将继续执行语句,即 Message 和Exit Sub

试试我上面的代码,并使用断点..希望这会有所帮助..


推荐阅读