Rancang form seperti dibawah ini :
Kode program :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim acak As New Random
Dim hsl, agka, gmbr As Integer
For a = 1 To Val(Me.TextBox1.Text)
hsl = acak.Next(2)
If hsl = 0 Then
agka = ganjil(agka + 1)
Else
gmbr = ganjil(gmbr + 1)
End If
Next
Me.TextBox2.Text = agka
Me.TextBox3.Text = gmbr
End Sub
Function ganjil(ByVal i As Integer) As Integer
Dim bal As Integer = 0
If i Mod 2 = 0 Then
Return i + 1
Else
Return i
End If
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Hasil :
Kode program :
Public Class Form1
Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click
Dim acak As New Random
Dim hsl, agka, gmbr As Integer
For a = 1 To Val(Me.TextBox1.Text)
hsl = acak.Next(2)
If hsl = 0 Then
agka = ganjil(agka + 1)
Else
gmbr = ganjil(gmbr + 1)
End If
Next
Me.TextBox2.Text = agka
Me.TextBox3.Text = gmbr
End Sub
Function ganjil(ByVal i As Integer) As Integer
Dim bal As Integer = 0
If i Mod 2 = 0 Then
Return i + 1
Else
Return i
End If
End Function
Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
End Sub
End Class
Hasil :
Post a Comment