Visual Basic循环问题

时间:2017-11-01 14:49:17

标签: vb.net

是否有人熟悉Bacon number?

现在有人熟悉这样一个概念:当你拼出一个数字时,它的值是4.例如,3是“三”。 “三”有5个字母。 “五”有4.每个数字将导致四个。我正在制作一个程序,跟踪到达四个步骤所需的步骤。

我目前做得很好,但我似乎可以在没有冻结的情况下重新运行该程序,最终表明它处于无限循环中。这是代码。

Public Class Form1
Dim intInput, intNumber, intCounter, intFinalCount As Integer
Dim strWord As String
Function count()
    For Each singleChar In strWord
        intCounter += 1
    Next
    Select Case intCounter
        Case 1
            strWord = "one"
        Case 2
            strWord = "two"
        Case 3
            strWord = "three"
        Case 4
            strWord = "four"
        Case 5
            strWord = "five"
        Case 6
            strWord = "six"
    End Select
End Function
Function count2()
    Select Case intCounter
        Case 1
            strWord = "one"
        Case 2
            strWord = "two"
        Case 3
            strWord = "three"
        Case 4
            strWord = "four"
        Case 5
            strWord = "five"
        Case 6
            strWord = "six"
    End Select
End Function
Private Sub btnPomeranz_Click(sender As Object, e As EventArgs) Handles btnPomeranz.Click
    intInput = 0
    intNumber = 0
    intFinalCount = 0
    strWord = ""
    intCounter = 0
    If txtKershaw.Text <> "" Then
        intInput = txtKershaw.Text
    Else
        Close()
    End If
    intNumber = intInput

    count2()
    count()


    Do While intCounter <> 4
        If intCounter <> 4 Then
            count()
            intFinalCount += 1
        End If
    Loop

    lblKluber.Text = intFinalCount

End Sub
End Class

3 个答案:

答案 0 :(得分:2)

查找代码和代码的不同之处并查看此内容以便学习 how to exit an app safely

你需要将你的计数重置为0。

   Function count()
        intCounter = 0
        For Each singleChar In strWord
            intCounter += 1
        Next

它应该读取intNumber而不是intCounter

Function count2()
    Select Case intCounter
        Case 1

这只会关闭表单并将您的应用留在后台。使用End代替

 If txtKershaw.Text <> "" Then
        intInput = txtKershaw.Text
    Else
        Application.Exit ' this is the correct way of closing your app. End() makes a hard exit and close() will never close your app until the thread is close
end if

工作代码

Public Class Form1
    Dim intInput, intNumber, intCounter, intBacon As Integer
    Dim strWord As String

    Private Sub btnPomeranz_Click_1(sender As Object, e As EventArgs) Handles btnPomeranz.Click
        intInput = 0
        intNumber = 0
        intBacon = 0
        strWord = ""
        intCounter = 0
        If txtKershaw.Text <> "" Then
            intInput = txtKershaw.Text
        Else
            End
        End If
        intNumber = intInput
        count2()
        count()


        Do While intCounter <> 4
            If intCounter <> 4 Then
                count()
                intBacon += 1
            End If
        Loop

        lblKluber.Text = intBacon
    End Sub

    Function count()
        intCounter = 0
        For Each singleChar In strWord
            intCounter += 1
        Next
        Select Case intCounter
            Case 1
                strWord = "one"
            Case 2
                strWord = "two"
            Case 3
                strWord = "three"
            Case 4
                strWord = "four"
            Case 5
                strWord = "five"
            Case 6
                strWord = "six"
        End Select
    End Function
    Function count2()
        Select Case intNumber
            Case 1
                strWord = "one"
            Case 2
                strWord = "two"
            Case 3
                strWord = "three"
            Case 4
                strWord = "four"
            Case 5
                strWord = "five"
            Case 6
                strWord = "six"
        End Select
    End Function
End Class

答案 1 :(得分:2)

你的问题在于在intCounter的初始值赋值之后:

intCounter = 0

intCounter没有在其他任何地方设置,并且将始终保持为0,因此

Do While intCounter <> 4
    If intCounter <> 4 Then
        count()
        intBacon += 1
    End If
Loop

将无限循环

唯一可以设置的地方是

For Each singleChar In strWord
    intCounter += 1
Next

strWord永远不会在""初始分配后设置,因此intCounter += 1永远不会执行

作为一方,你应该只有一个count()函数并将参数传递给它并让它返回结果。

此代码存在一些其他问题,但这不在问题范围内

答案 2 :(得分:2)

您可以简化代码并消除这样的错误:

Return

请注意,此代码按照预期的方式使用函数,将输入作为参数传递给它们,函数使用Count语句返回输出。 List(Of String)函数

  • 以当前号码为参数。
  • 如果数字无效(不在0到10之间),则返回0。
  • 否则返回表示数字的单词的长度。

我们使用name = numbers(number)来保存有效数字的名称。这使得检查数字是否在范围内变得容易,它必须是&gt; = 0并且&lt;列表的长度(请注意列表项从0开始编号)。它还可以轻松查找数字的名称(Integer.TryParse)。

按钮单击处理程序只需要检查输入TextBox是否包含整数(Count这样做以及将文本转换为数字)。然后它在循环中调用NULL,直到找到答案4。如果代码出现问题,循环会在100次尝试后退出(以避免无限循环)。