VB编程当输入一个正整数n时,判断n是否为素数,输出n为素数或合数
答案:2 悬赏:60 手机版
解决时间 2021-02-11 08:58
- 提问者网友:献世佛
- 2021-02-11 01:39
VB编程当输入一个正整数n时,判断n是否为素数,输出n为素数或合数
最佳答案
- 五星知识达人网友:夜余生
- 2021-02-11 03:01
Private Sub Command1_Click()
Dim i%, n%
n = Val(InputBox(""))
For i = 2 To n / 2
If n Mod i = 0 Then Exit For
Next
If i >= n / 2 Then
Print n; " is su shu"
Else
Print n; " is he shu"
End If
End Sub
Dim i%, n%
n = Val(InputBox(""))
For i = 2 To n / 2
If n Mod i = 0 Then Exit For
Next
If i >= n / 2 Then
Print n; " is su shu"
Else
Print n; " is he shu"
End If
End Sub
全部回答
- 1楼网友:轻雾山林
- 2021-02-11 03:48
添加如下控件
dim shu as long
private sub command1_click()
dim a as integer, w1 as integer, w2 as integer
w1 = 10 ^ val(text1)
w2 = 10 ^ (val(text1) - 1)
randomize
shu = int(rnd() * (w1 - w2)) + w2
label3.caption = "产生的随机数为:" + str(shu)
end sub
private sub command2_click()
label3.caption = just_ss(shu)
end sub
private sub form_load()
label1.caption = "随机生成一个"
label2.caption = "位整数"
label3.caption = ""
text1 = ""
command1.caption = "产生随机数"
command2.caption = "判 断"
end sub
function just_ss(a as long) as string
dim b as single, i as integer
b = a - 1
for i = 2 to b
if a mod i = 0 then
just_ss = cstr(a) + " 不是素数"
exit function
end if
next i
just_ss = cstr(a) + " 是素数"
end function
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯
正方形一边上任一点到这个正方形两条对角线的 |
阴历怎么看 ? |