2014-10-07 277 views
0

我是vb的初學者,我將在商店銷售中每100顯示*;但是,在循環中重新打印之前輸入的值。謝謝你的幫助。爲什麼我的循環無法正常工作visual basic

我要添加到列表如下:

  • 店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 

回答

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

剛剛修好。我改變

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

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