VB新手問問題.......
請問有方法可以簡化以下程式ㄇ?
1 a = inputbox("1+1")
if a = 2 then
msgbox "correct"
else
msgbox "error"
go to 1
end if
2 a = inputbox("5-2")
if a = 3 then
msgbox "correct"
else
msgbox "error"
go to 2
end if
3 a = inputbox("3*4")
if a = 12 then
msgbox "correct"
else
msgbox "error"
go to 3
end if
.
.
.
.
最近想寫一程式 ....
但是發現照上面寫很費時.....
不知有無高手能幫解......
感激不盡~~~~~~
2006-12-17 19:12:01 · 2 個解答 · 發問者 KCVO 1 in 電腦與網際網路 ➔ 程式設計
Private Sub Command1_Click()
Dim A, B%, C%, D%, Ans!, S$, K$
A = Array(" + ", " - ", " ", " ")
Randomize
Do
B = Int(Rnd * 9 + 1)
C = Int(Rnd * 9 + 1)
D = Int(Rnd * 4)
K = ""
Select Case D
Case 0: Ans = B + C
Case 1: Ans = B - C
Case 2: Ans = B * C
Case 3
Ans = Format(B / C, "0.0")
If Int(Ans) <> Ans Then: K = " (四捨五入到小數點後1位)"
End Select
S = InputBox(B & A(D) & C & " = ?" & K)
If StrPtr(S) = 0 Then Exit Sub '按取消離開
If S = CStr(Ans) Then
MsgBox "Correct"
Else
MsgBox "Error"
End If
DoEvents
Loop
End Sub
2006-12-19 10:29:43 · answer #1 · answered by W.J.S. 7 · 0⤊ 0⤋
這個可以連續解10題 +, -, *
/ 的話要小心
因為答案可能不是整數
Private Sub Command1_Click()
Dim a As Integer, b As Integer, i As Integer
Dim ans As Integer, user_ans As Integer, op As Integer
Dim buf As String
i = 10
Randomize ' Initialize random-number generator.
Do
a = Int(10 * Rnd) ' 0 ~ 9
b = Int(10 * Rnd) ' 0 ~ 9
op = Int(3 * Rnd) ' 0 ~ 2
If (op = 0) Then
buf = a & " + " & b & " = ?"
ans = a + b
ElseIf (op = 1) Then
buf = a & " - " & b & " = ?"
ans = a - b
Else
buf = a & " * " & b & " = ?"
ans = a * b
End If
user_ans = InputBox(buf)
If user_ans = ans Then
MsgBox "correct"
Else
MsgBox "error"
End If
i = i - 1
Loop While (i > 0)
End Sub
2006-12-17 20:55:24 · answer #2 · answered by JJ 7 · 0⤊ 0⤋