我该如何结束循环?

时间:2013-07-20 23:16:49

标签: vb.net

我做了一个循环,在输入名字时显示余额,但是当我第二次尝试这样做时。它不会返回另一个输出(messagebox)。如何结束/重启循环?

Public Class Form1
    Dim intMax_Subscript As Integer = 7
    Dim arrayNames(intMax_Subscript) As String
    Dim arrayBalance(intMax_Subscript) As Double
    Dim found As Boolean
    Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs)    Handles MyBase.Load
        CBO1.Items.Add("John One")
        arrayNames(0) = CStr(CBO1.Items(0))
        CBO1.Items.Add("Jack Two")
        arrayNames(1) = CStr(CBO1.Items(1))
        CBO1.Items.Add("John Three")
        arrayNames(2) = CStr(CBO1.Items(2))
        CBO1.Items.Add("Jack Four")
        arrayNames(3) = CStr(CBO1.Items(3))
        CBO1.Items.Add("John Five")
        arrayNames(4) = CStr(CBO1.Items(4))
        CBO1.Items.Add("Jack Six")
        arrayNames(5) = CStr(CBO1.Items(5))
        CBO1.Items.Add("John Seven")
        arrayNames(6) = CStr(CBO1.Items(6))
        CBO1.Items.Add("Jack Eight")
        arrayNames(7) = CStr(CBO1.Items(7))


    CBO2.Items.Add("235.50")
    arrayBalance(0) = CDbl(CBO2.Items(0))
    CBO2.Items.Add("78943.98")
    arrayBalance(1) = CDbl(CBO2.Items(1))
    CBO2.Items.Add("230781.10")
    arrayBalance(2) = CDbl(CBO2.Items(2))
    CBO2.Items.Add("78362.00")
    arrayBalance(3) = CDbl(CBO2.Items(3))
    CBO2.Items.Add("12097.20")
    arrayBalance(4) = CDbl(CBO2.Items(4))
    CBO2.Items.Add("89267.34")
    arrayBalance(5) = CDbl(CBO2.Items(5))
    CBO2.Items.Add("34959.06")
    arrayBalance(6) = CDbl(CBO2.Items(6))
    CBO2.Items.Add("559284.50")
    arrayBalance(7) = CDbl(CBO2.Items(7))
End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnBalance.Click
    Dim input As String = InputBox("Input a name")
    Dim intcount As Integer = -1
    Dim intSubscript As Integer = 0


    Do While Not found And intcount < arrayNames.Length
        If arrayNames(intSubscript) = input Then
            found = True
            MessageBox.Show("The balance of " & input & "'s is " & arrayBalance(intSubscript))
            intSubscript = 0
            intcount = -1
        End If
        intSubscript += 1
    Loop
    If Not arrayNames.Contains(input) Then
        MessageBox.Show("Error: Name not found.")
    End If
End Sub

2 个答案:

答案 0 :(得分:1)

您需要在显示消息框

后将找到的内容重置为false
If Not arrayNames.Contains(input) Then
MessageBox.Show("Error: Name not found.")
found = False
End If

答案 1 :(得分:1)

你根本不需要'found'变量

Do While intcount < arrayNames.Length

    If arrayNames(intSubscript) = input Then
        MessageBox.Show("The balance of " & input & "'s is " & arrayBalance(intSubscript))
        intSubscript = 0
        Exit Do       
    End If
    intSubscript += 1

Loop