Vb.net 为什么我的循环不能正常工作visual basic
我是vb的初学者,每100家店的销售额我就要显示一个*;但是,在循环中重新打印之前输入的值。谢谢你的帮助 我想在列表中添加以下内容:Vb.net 为什么我的循环不能正常工作visual basic,vb.net,Vb.net,我是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
- 商店1:**
- 商店2*****
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
公共课练习6
私有子btnDisplay\u单击(发送者作为对象,e作为事件参数)处理btnDisplay。单击
常量intNumberOfDays为整数=5'天
Dim intSales作为十进制=0'保存每日销售额
Dim strUserInput As String'以保存用户输入
暗条纹为字符串=“星号”
常量intAsterisk作为整数=100
Dim intAsteriskTotal为整数
Dim strDataOut As String=String.Empty'保存列表输出
整数为1的整数计数器
'获得5家店铺的销售额
当intCounter刚刚固定时执行。我变了
strDataOut &= "Store " & intCounter.ToString() & ": " & strAsterisks
与
代码中的问题是,您在每次迭代中都使用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
Do While intCounter
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