为什么我的循环不能正常工作视觉基础

时间:2014-10-07 23:17:19

标签: vb.net

我是vb的初学者,我每100个商店的销售额会显示一个*;但是,在循环中重新打印之前输入的值。谢谢你的帮助。

我想添加到列表中,如下所示:

  • 商店1:**
  • store 2 *****

每个* = 100

Public Class Exercise6

    Private Sub btnDisplay_Click(sender As Object, e As EventArgs) Handles btnDisplay.Click

        Const intNumberOfDays As Integer = 5           'days 
        Dim intSales As Decimal = 0             'to hold daily sales
        Dim strUserInput As String              'to hold user input
        Dim strAsterisks As String = ""          'Asterisks
        Const intAsterisk As Integer = 100
        Dim intAsteriskTotal As Integer
        Dim strDataOut As String = String.Empty    'holds the list output
        Dim intCounter As Integer = 1

        'gets sales for 5 stores
        Do While intCounter <= intNumberOfDays
            strUserInput = InputBox("Enter the sales for store" &
                                    intCounter.ToString(), "Store Sales is Needed")
            If strUserInput <> String.Empty And IsNumeric(strUserInput) Then
                intSales = CInt(strUserInput)


                'calculate the number of asterisk that must be display
                intAsteriskTotal = CInt(intSales / intAsterisk)

                strAsterisks = New String(CChar("*"), intAsteriskTotal)
                'add the store to the output string


                strDataOut &= "Store " & intCounter.ToString() & ": " & strAsterisks
                'add the output to the list box
                lstChart.Items.Add(strDataOut)
                intCounter += 1
            Else
                MessageBox.Show("Please enter a proper value for store sales.")
            End If

        Loop

    End Sub
End Class

2 个答案:

答案 0 :(得分:0)

您的代码中的问题是您使用strDataOut将每次迭代中的字符串附加到strDataOut &=。因此,如果您在循环strDataOut开始时清除strDataOut = "",那将是正确的。因此,您的代码将如下所示:

    Do While intCounter <= intNumberOfDays
        strUserInput = InputBox("Enter the sales for store" &
                                intCounter.ToString(), "Store Sales is Needed")
        strDataOut = "" '<---------- Additional line added
        If strUserInput <> String.Empty And IsNumeric(strUserInput) Then
            intSales = CInt(strUserInput)
            intAsteriskTotal = CInt(intSales / intAsterisk)
            strAsterisks = New String(CChar("*"), intAsteriskTotal)
            strDataOut &= "Store " & intCounter.ToString() & ": " & strAsterisks
            ListBox1.Items.Add(strDataOut)
            intCounter += 1
        Else
            MessageBox.Show("Please enter a proper value for store sales.")
        End If
    Loop

答案 1 :(得分:-1)

刚修好。我改变了

strDataOut &= "Store " & intCounter.ToString() & ": " & strAsterisks

strDataOut = "Store " & intCounter.ToString() & ": " & strAsterisks