用visual basic整估數字!

2010-04-20 2:25 am
i dind it in google!
Public nub As String

Private Sub Form_Load()

Call Random

End Sub

Private Sub Text1_KeyPress(KeyAscii As Integer)

If KeyAscii = 13 Then

If Val(Text1) > nub Then Label1.Caption = ("X < " & Text1)

If Val(Text1) < nub Then Label1.Caption = ("X > " & Text1)

If Val(Text1) = nub Then

Label1.Caption = ("X = " & Text1)

MsgBox ("bingo")

Call Random

End If

Text1.Text = ""

End If

End Sub

Function Random()

Randomize

nub = Int(Rnd * 99999)

Label1.Caption = ("X = ?")

End Function
is this code right??
if i want to to make the random number 1-100, how can i put the number into the code??
i am using vb 2008
thx a lot!
更新1:

my school said that should have 3 boxes , one from minimum one maximum and one of enter a number

回答 (1)

2010-04-21 11:47 pm
✔ 最佳答案
啲程式碼冇錯~~(不過係有很多不完善的地方)
而單憑佢將label用caption的方法~肯定佢係用vb6~
你如果係用vb2008~
就唔會咁寫~~
我會咁寫~~
會有一個button1,一個textBox1,一個label1
啲程式式是~
Public Class Form1
Dim rndnum As Integer

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
Randomize()
rndnum = Int(Rnd() * 100) + 1


End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

If Mid(Str(Val(TextBox1.Text)), 2) <> TextBox1.Text Then
MsgBox("不可以輸入字串!")

ElseIf Mid(Str(Int(Val(TextBox1.Text))), 2) <> TextBox1.Text Then
MsgBox("不可以輸入小數!")
Else


If Val(TextBox1.Text) >= 1 And Val(TextBox1.Text) <= 100 Then


If rndnum > Val(TextBox1.Text) Then
Label1.Text = "X >" & Val(TextBox1.Text)
ElseIf rndnum < Val(TextBox1.Text) Then
Label1.Text = "X <" & Val(TextBox1.Text)
ElseIf rndnum = Val(TextBox1.Text) Then
Label1.Text = "you win! "
rndnum = Int(Rnd() * 100) + 1
End If
Else
MsgBox("要輸入1-100的整數!")
End If

End If
End Sub


End Class
這已做了跟你放上來的差不多的了~~而且在textBox中不可以打1-100以外的東西~如果打了~在按button時會有massage box彈出來鬧你~~

2010-04-22 00:00:29 補充:
我這些程式碼~已是form1.vb內的所有code~~如果你一開個form~~內裡已有一些程式碼~~你如果唔delete佢~~再貼我俾你的code~~咁一定會有問題啦~~重有我想問下~~你其實睇唔睇得明你找的那些程式碼~~和我給你的那些程式碼~~如果咁都睇唔明~~我諗你真係初學很好初學~~如果你係諗住學寫program~~你點都要睇明佢~~~~

2010-04-22 00:00:50 補充:
而你講"have 3 textbox, one is minimum one is maxium and one for enter the number!! "~~其實唔須要~~用返嗰個label1就可以~~
先dim兩個數~~
dim maxnum as integer
dim minnum as integer

2010-04-22 00:02:09 補充:
然後~~

If rndnum > Val(TextBox1.Text) Then
...
...
End If
改成~~
參考: 自己多年來的經驗~~不過這個問題太過簡單了~


收錄日期: 2021-04-30 23:02:39
原文連結 [永久失效]:
https://hk.answers.yahoo.com/question/index?qid=20100419000051KK01041

檢視 Wayback Machine 備份