【问题标题】:why is my loop not working properly visual basic为什么我的循环不能正常工作 Visual Basic
【发布时间】:2014-10-08 21:00:04
【问题描述】:

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

我想添加如下列表:

  • 商店 1:**
  • 商店 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

【问题讨论】:

    标签: vb.net


    【解决方案1】:

    您的代码中的问题是您在每次迭代中使用strDataOut &amp;= 将字符串附加到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
    

    【讨论】:

      【解决方案2】:

      刚刚修复。我变了

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

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

      【讨论】:

        猜你喜欢
        • 2018-05-23
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-05-16
        • 1970-01-01
        • 2014-10-15
        相关资源
        最近更新 更多