1樓:微標科技
'新增窗體form1,文字框text1,按鈕command1,command2,然後新增如下**:
private sub form_load()command1.caption = "產生隨機數"
command2.caption = "判斷"
text1.locked = false
a = false
end sub
private sub command1_click()text1.text = int(rnd() * 100)end sub
private sub command2_click()if not a then
msgbox "請產生一個隨機數!", vbexclamationexit sub
else
msgbox iif(val(text1.text) mod 2 = 0, "是偶數", "是奇數"), vbinformation
a = false
end if
end sub
private sub text1_change()a = true
end sub
2樓:秋色烽火
你設定一個變數 用來記錄隨機數 如果產生了就賦值 否則在判斷時判斷這個變數是否為空 為空則未產生
編寫vb程式:用隨機函式生成20個數值,並求出其中的奇數和與偶數和 還
3樓:麥兜
private sub form_load()
dim i%, n&(), a%, b%, x
randomize
n = random(0, 20, 21)
print "偶數:"
for i = 0 to ubound(n)
if n(i) mod 2 = 0 then
a = a + n(i)
print n(i);
x = x + 1
if x > 5 then print: x = 0
end if
next
x = 0
print "奇數:"
for i = 0 to ubound(n)
if n(i) mod 2 = 1 then
b = b + n(i)
print n(i);
x = x + 1
if x > 5 then print: x = 0
end if
next
print vbcrlf
print "偶數和:"; a
print "奇數和:"; b
end sub
public function random(byval min&, byval max&, byval count%) as variant
dim i&, x&, n$, regexp as object, matches as object, match as variant
redim num&(count - 1)
set regexp = createobject("vbscript.regexp")
with regexp
.global = -1
for i = 1 to count
x = (max - min) * rnd + min
n = n & x & chr(32)
if i > 1 then
.pattern = "\b" & x & "\b"
set matches = .execute(n)
if matches.count > 1 then
.pattern = "\b\d+\b $"
n = .replace(n, empty)
i = i - 1
end if
end if
next
n = left(n, len(n) - 1)
.pattern = "\d+"
set matches = .execute(n)
for match = 0 to matches.count - 1
num(match) = matches(match)
next
end with
random = num
set regexp = nothing: set matches = nothing
end function
4樓:數學與計算機程式設計
private sub command1_click()
randomize (timer)
label1.caption = ""
label2.caption = ""
label3.caption = ""
for i = 1 to 20
n = int(rnd * 9000 + 1000)
label1.caption = label1.caption + cstr(n) + space(1)
if i mod 10 = 0 then label1.caption = label1.caption + vbcrlf
if n mod 2 = 0 then
m = m + 1
label2.caption = label2.caption + cstr(n) + space(1)
if m mod 10 = 0 then label2.caption = label2.caption + vbcrlf
evennumber = evennumber + n
else
p = p + 1
label3.caption = label3.caption + cstr(n) + space(1)
if p mod 10 = 0 then label3.caption = label3.caption + vbcrlf
oddnumber = oddnumber + n
end if
next i
label2.caption = label2.caption + vbcrlf + "偶數和=" + cstr(evennumber)
label3.caption = label3.caption + vbcrlf + "奇數和=" + cstr(oddnumber)
end sub
編寫vb程式 產生20個隨機數 輸出這20個數 分別計算這20個數的奇數之和和偶數只和 謝謝了
5樓:聽不清啊
private sub command1_click()s1 = 0: s2 = 0
for i = 1 to 20
x = int(rnd * 101)
if x mod 2 = 1 then s1 = s1 + x else s2 = s2 + x
print x,
if i mod 5 = 0 then printnext i
print "奇數之和="; s1
print "偶數之和="; s2
end sub
vb6.0隨機生成一個數問題
6樓:匿名使用者
先把生成的概率變數加起來,如果小於100,則變成100.然後按100的隨機數生成,比如:1.
2.3.4.
5.6的概率,生成的數在1-1(概率1),顯示第一個數,如果是2-3(概率2),顯示第二個數,如果是4-6(概率3),顯示第三個數...
7樓:設計營地
這個簡單,只要知道概率,先生成0~1之間的隨機數,只要概率符合,就把相應的數賦給相應的變數。如果概率為1,就直接新增進去就行了。
用vb編寫程式,隨機生成20個三位正整數,將其中的奇數與偶數分別輸出到兩個列表框中。
8樓:匿名使用者
'新增兩個列表框,示例**如下:
private sub form_click()dim a(1 to 20)
randomize timer
for i = 1 to 20
a(i) = int(rnd * 900 + 100)print a(i); " ";
if a(i) mod 2 = 0 thenlist2.additem a(i)
else
list1.additem a(i)
end if
next i
end sub
9樓:數學與計算機程式設計
private sub command1_click()list1.clear
list2.clear
for i = 1 to 20
n = int(rnd * 900 + 100)if n mod 2 = 1 then
list1.additem n
else
list2.additem n
end if
next i
end sub
10樓:有郊範圍
private sub command1_click()dim s as integer, i as integerfor i = 1 to 20
randomize
s = int(rnd * 900 + 100)if s mod 2 = 0 then list2.additem s else list1.additem s
next i
end sub
VB6怎麼用錯誤捕獲,VB中程式錯誤的捕獲以及處理方法
vb6使用on error 語句來捕獲程式執行中的可預見錯誤或不可預見的錯誤。on error 語句,啟動一個錯誤處理程式並指定該子程式在一個過程中的位置 也可用來禁止一個錯誤處理程式。說明如果不使用 on error 語句,則任何執行時錯誤都是致命的 也就是說,結果會導致顯示錯誤資訊並中止執行。o...
vb6中怎麼修改字型顏色,VB中如何改變文字顏色
可以直接在bai 屬性面板裡設定對 象的du zhiforecolor 如果有 屬性。用 設定dao,其內格式 容物件.forecolor 設定值其中設定值可以為四種 1 例如 物件.forecolor vbred 紅色,或vbblue等,vbgreen綠色等 2 rgb 正如樓上 爛掉 蘿蔔 所言...
win10支援vb6開發的程式嗎
1本文以vb6.0企業版為例,其他版本同此理!進入安裝程式資料夾,找到setup檔案,點選滑鼠右鍵 屬性,如下圖所示 2在屬性介面選擇相容性 勾選以相容模式執行此程式,選擇xp,點選應用按鈕,如下圖所示 3繼續滑鼠右鍵單擊setup,選擇以管理員身份執行程式,如下圖所示 4啟動安裝,點選下一步,如下...