如何使用VBScript使Randomizer做出更多动作?

时间:2016-08-07 22:55:45

标签: vbscript

我正在尝试制作一个将从5中选择一个动作的随机函数。但问题是,当我运行脚本时,它不会从五个中选择一个动作。它只是在第3行运行动作。这是我完成的脚本:

Randomize   
 a=Int((5 * Rnd()) + 1)   
 If a=5 then dim result
result = msgbox("Are you sure you want to install?", 4 , "Select yes or no")
 If a=4 then c="Yes"  
 If a=3 then c="No"   
 If a=2 then c="Maybe"   
 If a=1 then c="Ask Me Later"  
 b=inputbox("What Is Your Question?") 
 c=msgbox("" & c)

这些代码行被认为是一种具有秘密的各种神谕。

2 个答案:

答案 0 :(得分:1)

编辑:

上帝,这很令人尴尬: 实际上我刚刚测试了你的代码,aaaaand:它对我来说很好。也许你应该再次指明你的问题了?!

回答 Randomize() a=Int((5 * Rnd()) + 1) Select Case a Case 1 c= "Ask Me Later" Case 2 c= "Maybe" Case 3 c = "No" Case 4 c = "Yes" Case 5 dim result result = msgbox("Are you sure you want to install?", 4 , "Select yes or no") End Select b = Inputbox("What Is Your Question?") c = MsgBox("" & c)

def function(x):
    y = x + 1
    return y

Input = raw_input('Number?')

with open('in_test.txt','w+') as inFile_test:
    inFile_test.write(Input)
    lines = inFile_test.readline()
    lines_int = [int(x) for x in lines.split()]
    print str(lines_int)

f_test = function(lines_int)
print str(f_test)

答案 1 :(得分:0)

您的代码使用单行If..Then语句,您无法分配多个操作。试试这个:

Randomize   
 a=Int((5 * Rnd()) + 1)   
 If a=5 then 
     dim result
     result = msgbox("Are you sure you want to install?", 4 , "Select yes or no")
 End If
 If a=4 then c="Yes"  
 If a=3 then c="No"   
 If a=2 then c="Maybe"   
 If a=1 then c="Ask Me Later"  
 b=inputbox("What Is Your Question?") 
 c=msgbox("" & c)

值得注意的是(如在其他答案和评论中),如果您有多个If语句,使用这样的select case语句通常会更好(I&I #39; ve还重命名了变量以便于阅读:)

Randomize   
randomNum=Int((5 * Rnd()) + 1)   
Select Case(randomNum)
    Case 1 : randomResponse = "Ask Me Later"
    Case 2 : randomResponse = "Maybe"
    Case 3 : randomResponse = "No"
    Case 4 : randomResponse = "Yes"
    Case 5 : Dim result
             result = MsgBox("Are you sure you want to install?", 4, "Select Yes or No") ' this option seems unwanted given your desired behaviour
End Select
userQuestion=InputBox("What Is Your Question?") 
' should probably do something with userQuestion 
' if it's empty you still return the random answer from above
' so only return the response where it's not
If Len(userQuestion)> 0 Then
    MsgBox(randomResponse) ' don't need to append an empty string or set it to anything
End If