用于保龄球得分阵列的循环

时间:2015-01-27 19:51:15

标签: vb.net

我正在尝试创建一个保龄球程序,该程序将显示多行文本框中给出的分数。我已经设法让程序给出一个输出,但是当它运行时它跳过要求新的输入并且在单独的行上给出5 0并且没有别的。我完全迷失了,非常感谢任何帮助

编辑:抱歉应该更改错误以反映程序更改,现在看起来像这样。它给出0而不是使用我给它的值,但现在它确实要求每个输入。

For gameNumber As Integer = 1 To 5 Step 1
        lblEnterScore.Text = "Enter Score for game #" & gameNumber

        Dim Testint As Integer  ' define an Integer for testing

        Try
            Testint = CInt(txtScoreInput.Text)     ' try to convert whatever they entered to Int

        Catch
            MessageBox.Show("Entry is not an Integer")   ' If you are here then the CInt failed for some reason, send a message
            txtScoreInput.SelectAll()
            txtScoreInput.Focus()
            Exit Sub
        End Try
        If txtScoreInput.Text.Contains(".") Then
            MsgBox("Bowling Score must be a whole number.")
            txtScoreInput.SelectAll()
            txtScoreInput.Focus()
            Exit Sub
        End If
        If txtScoreInput.Text > MAXIMUM_SCORE Or txtScoreInput.Text < MINIMUM_SCORE Then
            MsgBox("Bowling Score must be between 1 and 300.")
            txtScoreInput.SelectAll()
            txtScoreInput.Focus()
            Exit Sub
        End If

        scoreInput(gameNumber) = CInt(txtScoreInput.Text)
        ' and store it in the array

        ' and increment the gamecounter for the next time through the loop
    Next
    'btnEnterScore.Enabled = False
    ' place the good score into the multi-line textbox
    txtScoreOutputs.Text = gameScore & vbCrLf & txtScoreOutputs.Text

End Sub

2 个答案:

答案 0 :(得分:1)

如果是我,这就是我要做的......只是一个建议;我还删除了超过一半的代码并阻止它抛出异常......你可以把它放在点击事件中或者你需要它的地方。您也可以修改它以从用户输入中获取任意数量的内容,而不仅限制它们输入分数。您的用户也可以选择在他们选择这样做时退出该循环,而不是让他们保持在循环内...

Private ScoreLists As New List(Of Integer) 'Hold your inputted values
Private Const MAXIMUM_SCORE As Integer = 300 'Max score
Private Const MINIMUM_SCORE As Integer = 1 'Min score
Private blnStop As Boolean = False

        Try
            For gameNumber As Integer = 1 To 5
                Dim intScore As Integer = 0

                Do Until (intScore >= MINIMUM_SCORE And intScore <= MAXIMUM_SCORE) OrElse blnStop
                    If Not Integer.TryParse(InputBox("Please enter a score for game # " & gameNumber.ToString), intScore) Then
                        If MsgBox("Bowling Score must be a whole number. Stop getting scores?.", MsgBoxStyle.YesNo) = MsgBoxResult.Yes Then
                            blnStop = True
                            Exit For
                        End If
                    End If
                Loop

                ScoreLists.Add(intScore)
            Next

            'Display the results...
            For i As Integer = 0 To ScoreLists.Count - 1
                txtScoreOutputs.Text &= ScoreLists.Item(i.ToString)
            Next

            ScoreLists.Clear()

        Catch ex As Exception
        End Try

答案 1 :(得分:-1)

构造你的逻辑(伪):

Loop
{
    AskUserForInput()
    ProcessUserInput()
}

循环部分将控制提示用户输入的分数。 AskUserForInput()函数将提示用户输入新的分数,ProcessUserInput()函数将获取值并将其存储在数组中或将其打印到屏幕上等。

在尝试添加分数之前,您当前的逻辑不会等待任何新用户输入。您还获得了零,因为您正在使用gameScore设置txtScoreOutputs,这看起来并没有被设置为用户的输入。