编辑-如果其他语句变量问题如何解决?

时间:2019-04-06 21:36:03

标签: vbscript

我正在尝试进行随机数字游戏,但是即使我添加了b =输入框语句,条件始终为假

Option Explicit

dim b,a,max,min
'To randomize variable (a)
max=3
min=1
Randomize
a = (Int((max-min+1)*Rnd+min))
b = inputbox("Guess a number from " & min & " to " & max)
If a = b Then
    msgbox("you win")
Else 
    msgbox("you died it was " & a)
End If

我希望当您猜到正确的数字时,它会告诉您何时出现,但总是让您丧命的数字是#

2 个答案:

答案 0 :(得分:1)

您快到了,但是与been mentioned in the comments一样,您也没有在变量be上填充值,因此比较将始终为False

如果您期望用户填充b,则可以通过添加一行来通过InputBox()函数要求输入;

Option Explicit

Dim beans, b, a, max, min
'To randomize variable (a)
max = 100
min = 1
Call Randomize()
'Enter the line below to collect input from the user.
b = InputBox("Enter a number between " & min & " and " & max & ".")
'Remember to round the number to make sure you have a whole number.
a = Round((Int((max - min + 1) * Rnd() + min)))
If (a = b) Then
    Call MsgBox("You win")
Else 
    Call MsgBox("You died it was " & a)
End If

您还可以考虑验证输入,以确保用户在您的minmax之间输入一个值,如果该值无效,则做出相应的响应。

答案 1 :(得分:0)

这匹配1-10。

Randomize
Num = Int((10 - 1 + 1) * Rnd + 1)
If CInt(Inputbox("Enter Number")) = Num Then
    Msgbox "match"
Else
    Msgbox "Nope it was " & Num
End If

帮助的公式为Int((upperbound - lowerbound + 1) * Rnd + lowerbound)。参见http://download.microsoft.com/download/winscript56/Install/5.6/W982KMeXP/EN-US/scrdoc56en.exe