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
如果我一步一步地调试控件进入循环我的代码工作正常。 但是当我直接执行它时,控件不会进入循环
答案 0 :(得分:0)
Dim value As Integer = Integer.Parse(TextBox1.Text)
请确保TextBox1具有非零积分值。
答案 1 :(得分:0)
以下将产生更好的结果:
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
将Dim rnd
移出循环会停止代码在每次循环执行时创建一个新的随机对象,这意味着您获得了更好的结果,就像使用代码一样,获得{{1} } = TextBox6.Text
或value
= 0
同样使用TextBox6.Text
而不是object.Value += 1
更整洁,并且可以在大循环中提高执行速度。