vb程式設計 利用隨機函式產生10 99之間的隨機整數

時間 2021-08-30 10:29:12

1樓:匿名使用者

option explicit

private sub command1_click()

dim a(80) as integer, b() as integer, nb as integer

dim i as integer, j as integer, t as integer

'隨機生成80個隨機數

text1.text = ""

randomize

for i = 1 to 80

a(i) = int(90 * rnd) + 10

text1.text = text1.text & a(i) & " "

'每顯示十個數字換行

if i mod 10 = 0 then text1.text = text1.text & chr(13) & chr(10)

next i

'查詢奇數併到新陣列中

for i = 1 to 80

if a(i) mod 2 = 1 then

nb = nb + 1

redim preserve b(nb)

b(nb) = a(i)

end if

next i

'對奇數排序

for i = 1 to nb - 1

for j = i + 1 to nb

if b(i) > b(j) then

t = b(i)

b(i) = b(j)

b(j) = t

end if

next j

next i

'輸出奇數到文字框

text2.text = ""

for i = 1 to nb

text2.text = text2.text & b(i) & " "

if i mod 10 = 0 then text2.text = text2.text & chr(13) & chr(10)

next i

'查詢輸出素數

text3.text = ""

t = 0

for i = 1 to 80

if sushu(a(i)) = true then

t = t + 1

text3.text = text3.text & a(i) & " "

if t mod 10 = 0 then text3.text = text3.text & chr(13) & chr(10)

end if

next i

end sub

private function sushu(n as integer) as boolean '判斷某數是否素數

dim i as integer, p as integer

p = int(sqr(n))

for i = 2 to p

if n mod i = 0 then exit for

next i

if i > p then sushu = true

end function

窗體上放text1,text2,text3,command1,文字框要設定換行和滾動條

2樓:guoxin愛記錄

dim a(1 to 80)

randomize

for i =1 to 80

a(i)=int(rnd*80)+10

print a(i)

next i

vb程式設計題: 隨機生成一個4行9列的矩陣,矩陣元素為10-99之間的隨機整數.列印顯示.

3樓:山舟

option base 1

private sub form_click()dim a(36) as integer, b(6, 6) as integer, i as integer, j as integer, num as integer

print "原矩陣:"

randomize

for i = 1 to 36

a(i) = int((99 - 10 + 1) * rnd + 10)

print a(i);

num = num + 1

if num mod 9 = 0 then printnext

for i = 1 to 35

for j = 1 to 36 - i

if a(j) > a(j + 1) thent = a(j)

a(j) = a(j + 1)

a(j + 1) = t

end if

next

next

print "排序後矩陣:"

for i = 1 to 36

print a(i);

num = num + 1

if num mod 6 = 0 then printnext

print "主對角線上數字為:"

for i = 1 to 6

print a((i - 1) * 6 + i);

next

print

end sub

隨機函式產生100個10-99之間的隨機整數

4樓:百度文庫精選

內容來自使用者:範衛超10

利用隨機函式產生100個10-99之間的隨機整數,找出其中的奇數和素數,並將奇數按由小到大的順序排列。

private sub command1_click()dim a(1 to 100) as integer, b() as integer

show

redim b(100) as integerrandomize timer

print "隨機產生的資料是:"

for i = 1 to 100

a(i) = int(rnd * 90 + 10)print a(i); " ";

if i mod 10 = 0 then printnext i

print "找到的素數是:"

for i = 1 to 100

if fnprime(a(i)) thenprint a(i); " ";

c = c + 1

if c mod 10 = 0 then printend if

next i

print

print "找到的奇數是:"

for i = 1 to 100

if a(i) mod 2 = 1 thenk = k + 1

b(k) = a(i)

end if

next i

redim preserve b(k)

for i = 1 to k

print b(i); " ";

d = d + 1

if d mod 10 = 0 then printnext i

for i = 1 to k - 1

for j = i + 1 to k

if b(i) > b(j) then

t = b(i): b(i) = b(j): b(j) = tend if

next j

next i

print

print "排序結果:"

for j = 1 to k

print b(j);

g = g + 1

if g mod 10 = 0 then printnext j

end sub

private sub form_load()end sub

function fnprime(m as integer) as boolean

dim e as integer, f as booleanf = true

for e = 2 to m - 1

if m mod e = 0 then

急求vb程式設計「利用隨機函式產生10個50-100之間的隨機整數,求它們的最大值和平均值,要求用陣列做」,謝謝

5樓:匿名使用者

private sub command1_click()randomize

n=rnd()*10

for i=1 to n

s=s+i

a=a+1/s ''這裡應該注意!!!!!

next

msgbox "n值為"+str(n)+",結果為"+str(a)end sub

6樓:

你沒給分啊!!吃白飯,不行咧

vb編寫一個程式,利用隨機函式rnd生成10個10-99之間的整數,並計算其中素數的個數 10

7樓:匿名使用者

n= [ 10+int(90*rnd) ]st = [ st + " " + n ]forj=2 to [ n ]

[ n1=n1+1 ]

[ exit for ]

text1.text=[ st ]text2.text= [ n-n1 ]

8樓:

private sub command1_click()dim i as integer, j as integer, n as integer, n1 as integer, st as string

for i = 1 to 10

n =_

屬st = _

for j = 2 to _

if n mod j = 0 then_

next

if j > sqr(n) then

_end if

next

text1.text = _

text2.text = _

end sub

用vb編寫利用隨機函式產生10 99之間(包括10和

dim n m,a 19 max max 0 for n 0 to 19 a n int rnd 90 10 生成20個10 99的隨機數 next n for n 0 to 18 for m n 1 to 19 if a n a m then swap a n a m 排序從小到大end if n...

在vb中利用隨機函式產生10 99之間的隨機整數,找

玫瑰為你盛開 正好寫過這個哦,也給你好啦 以下 不需要任何控制元件 private sub form activate dim a 1 to 100 as integer,prime 1 to 100 as integer,np as integer,sp as integer a陣列存放100個隨...

vb程式設計應用二維陣列產生隨機矩陣55並實現矩陣

珈藍惜夢 vb源程式如下 option base 1 private sub command1 click dim a 5,5 as integer,b 5,5 as integer for x 1 to 5 for y 1 to 5 a x,y int 10 rnd 90 隨機產生兩位數的整數 p...