视觉基本循环问题

时间:2018-12-03 06:58:53

标签: vb.net vb.net-2010

我是学习Visual Basic的新程序员。现在,我正在做一个有关垒球记分牌的项目。我已经在这个项目上工作了一段时间,但我对一件事情感到困惑。

让我感到困惑的是放置循环的适当位置。我正在尝试执行while循环,但是当我尝试它时,它允许我进入7局。但是一旦执行,它是一个无限循环,并显示消息,仅允许7局,并且不显示lblTotal。它没有循环运行,但是却不允许我背对背输入所有局。如果您能帮助我,我将不胜感激。我觉得我将循环放置在错误的位置。

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
        Do While runs <= 7
            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
        Loop
        '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
    Private Sub mnuClear_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 mnuExit_Click(sender As Object, e As EventArgs) Handles mnuExit.Click
        'close application
        Application.Exit()
    End Sub
End Class

1 个答案:

答案 0 :(得分:0)

Do While runs <= 7

'runs'变量应该在'Do While'循环内进行更新(添加+1),以便能够满足条件并爆发。

   (...)
   runs += 1
Loop