2015-04-05 114 views
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 = 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 

如果我一步步调试控制进入循环我的代码工作正常。 但是,当我执行它直接控制不会在去循环代码没有进入For循环或做while循环

+0

在循环中放置一个断点并按F5确认执行命中循环。 – Senthil 2015-04-05 03:09:55

+0

如果存在中断点,则执行命中循环。但是,一旦我删除了中断点,它不会触及循环。 – meghan 2015-04-05 03:40:28

+0

这意味着它在执行时间内工作正常。可能是一些逻辑问题,不能按照您的预期解决结果。检查你的逻辑和非常天真 – Senthil 2015-04-05 03:43:49

回答

0

昏暗的值作为整数= Integer.Parse(TextBox1.Text)

请确保TextBox1中由具有非零整数值。

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圈外的停止代码每执行一次循环,创建一个新的随机对象,这意味着你得到一个更好的结果,与你的代码是很常见的获得或者TextBox6.Text = value OR TextBox6.Text = 0

而且使用object.Value += 1而不是object.Value = object.Value + 1是整洁,可以在大循环提高执行的速度。