当前位置:编程学习 > VB >>

VB编程 题

利用随机函数,模拟投币结果.设共投币100次,求"两个正面","两个反面","一正一反"3种情况各出现多少次.
答案:
Private Sub Command1_Click()
Dim i As Integer
Dim a As Integer
Dim b As Integer
Dim x As Integer
Dim y As Integer
Dim z As Integer

Randomize
For i = 1 To 100
a = Int(Rnd * 2) '产生0和1,0表示反面,1表示正面
b = Int(Rnd * 2)
If a + b = 2 Then
x = x + 1
ElseIf a + b = 0 Then
y = y + 1
ElseIf a + b = 1 Then
z = z + 1
End If
Next
Print "两个正面:" & x
Print "两个反面:" & y
Print "一正一反:" & z
End Sub
private sub command1.click()
dim m as integer,n as integer,k as integer,i as integer,t as integer
m=0;n=0;k=0
for i=1 to 100
t=(int)rnd()*2
s=(int)rnd()*2
if t=1 & s=1 then
m=m+1
elseif t=0 & s=0 then
n=n+1
else
k=k+1
next i
end sub
其中m是两个正面个数","n是两个反面个数","k是一正一反个数'

上一个:VB编程入门
下一个:vb编程请判断

CopyRight © 2022 站长资源库 编程知识问答 zzzyk.com All Rights Reserved
部分文章来自网络,