vb:输出150-280之间的质数,求这些质数的总和及平均数。急急急急!!!!!
答案:4 悬赏:30 手机版
解决时间 2021-03-14 18:09
- 提问者网友:皆是孤独
- 2021-03-14 13:01
用for 和if语句 ,拜托了
最佳答案
- 五星知识达人网友:轻雾山林
- 2021-03-14 13:34
Private Sub Command1_Click()
For i = 150 To 280
flg = True
For j = 2 To i - 1
If i Mod j = 0 Then
flg = False
End If
Next j
If flg = True Then
Sum = Sum + j
Print j;
n = n + 1
If n Mod 5 = 0 Then Print
End If
Next i
Print
m = Sum / n
Print "质数总和:" & Sum
Print "平均值:" & m
End Sub
For i = 150 To 280
flg = True
For j = 2 To i - 1
If i Mod j = 0 Then
flg = False
End If
Next j
If flg = True Then
Sum = Sum + j
Print j;
n = n + 1
If n Mod 5 = 0 Then Print
End If
Next i
m = Sum / n
Print "质数总和:" & Sum
Print "平均值:" & m
End Sub
全部回答
- 1楼网友:酒安江南
- 2021-03-14 15:30
for i=150 to 280
p=0
for j=2 to i-1
if i mod j =0 then p=1
next j
if p=0 then
s=s+i
t=t+1
endif
next i
ave=s/t 'ave为平均数 s为总和
- 2楼网友:長槍戰八方
- 2021-03-14 15:12
dim c as integer dim d as double
private sub command1_click() if c = 0 then text1.text = "îþ½â" else text2.text = str(d / c) end if end sub
private sub text2_keypress(keyascii as integer) dim t as double if keyascii = 13 then t = val(text2.text) if t <= 100 and t >= 0 then d = d + t c = c + 1 list1.additem str(t) end if text2.text = "" end if end sub
这样写。因为接受回车的控件应该是text2才对。你写list_keypress当然不行
再者,t,d两个变量要设置成全局变量否则只在keypress的过程中有效。
- 3楼网友:洒脱疯子
- 2021-03-14 14:25
Private Sub Command1_Click()
For i = 150 To 280
For j = 2 To Sqr(i)
If i Mod j = 0 Then Exit For
Next
If j > Sqr(i) Then
Sum = Sum + i
n = n + 1
End If
Next
Print "150-280之间的质数的总和="; Sum
Print "平均数="; Sum / n
End Sub
我要举报
如以上问答信息为低俗、色情、不良、暴力、侵权、涉及违法等信息,可以点下面链接进行举报!
大家都在看
推荐资讯