Vb.net 代码不进入For循环或Do while循环 Private子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击 如果Val(TextBox5.Text)=0,则 TextBox5.Text=0 如果结束 TextBox5.Text=TextBox5.Text+1 百分之二 百分比=(TextBox3.Text)/100 作为整数的Dim值=整数.Parse(TextBox1.Text) Dim标志为整数=0 Dim计数为整数=0 当计数
如果我一步一步地调试,控制进入循环,我的代码运行良好。Vb.net 代码不进入For循环或Do while循环 Private子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击 如果Val(TextBox5.Text)=0,则 TextBox5.Text=0 如果结束 TextBox5.Text=TextBox5.Text+1 百分之二 百分比=(TextBox3.Text)/100 作为整数的Dim值=整数.Parse(TextBox1.Text) Dim标志为整数=0 Dim计数为整数=0 当计数,vb.net,loops,debugging,Vb.net,Loops,Debugging,如果我一步一步地调试,控制进入循环,我的代码运行良好。 但是,当我直接执行它时,控件不会以Integer=Integer.Parse(TextBox1.Text)的形式进入循环 请确保TextBox1具有非零整数值。以下操作将产生更好的结果: Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click If Val(TextBox5.Text) = 0 Then
但是,当我直接执行它时,控件不会以Integer=Integer.Parse(TextBox1.Text)的形式进入循环
请确保TextBox1具有非零整数值。以下操作将产生更好的结果:
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Val(TextBox5.Text) = 0 Then
TextBox5.Text = 0
End If
TextBox5.Text = TextBox5.Text + 1
Dim percent As Double
percent = (TextBox3.Text) / 100
Dim value As Integer = Integer.Parse(TextBox1.Text)
Dim flag As Integer = 0
Dim count As Integer = 0
Do While count < value
Dim rnd = New Random()
Dim nextValue = rnd.Next(100) / 100
If nextValue < percent Then
flag = flag + 1
End If
count = count + 1
Loop
TextBox6.Text = value - flag
End Sub
Private子按钮1\u单击(发送者作为对象,e作为事件参数)处理按钮1。单击
如果Val(TextBox5.Text)=0,则
TextBox5.Text=0
如果结束
TextBox5.Text+=1
双精度尺寸百分比=(TextBox3.Text)/100
作为整数的Dim值=整数.Parse(TextBox1.Text)
Dim标志为整数=0
Dim计数为整数=0
Dim rnd=New Random()'将其移出循环
当计数<值时执行
尺寸nextValue=rnd.下一个(100)/100
如果nextValue<百分比,则
标志+=1
如果结束
计数+=1
环
TextBox6.Text=值-标志
端接头
将Dim rnd
移出循环会在每次循环执行时停止代码创建新的随机对象,这意味着您可以获得更好的结果,因为您的代码通常会获得TextBox6.Text
=值
或TextBox6.Text
=0
另外,使用
object.Value+=1
而不是object.Value=object.Value+1
更整洁,可以在大循环中提高执行速度。在循环中设置断点,然后按F5确认执行命中循环。是的,如果存在断点,则执行命中循环。但一旦我删除了断点,它就不会进入循环,这意味着它在执行时工作得很好。可能是一些逻辑问题,无法解决您预期的结果。检查你的逻辑和它的非常幼稚,你能解释一下为什么你认为循环没有被执行吗?我想你有不同的问题。谢谢。当我在上面测试您的代码时,您能否提供TextBox1
、TextBox3
的值以及您在Textbox6
中获得的输出,正如我所期望的那样。
Private Sub Button1_Click(sender As Object, e As EventArgs) Handles Button1.Click
If Val(TextBox5.Text) = 0 Then
TextBox5.Text = 0
End If
TextBox5.Text += 1
Dim percent As Double = (TextBox3.Text) / 100
Dim value As Integer = Integer.Parse(TextBox1.Text)
Dim flag As Integer = 0
Dim count As Integer = 0
Dim rnd = New Random() 'Move this out of the loop
Do While count < value
Dim nextValue = rnd.Next(100) / 100
If nextValue < percent Then
flag += 1
End If
count += 1
Loop
TextBox6.Text = value - flag
End Sub