2015-04-05 112 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是整潔,可以在大循環提高執行的速度。