大家好我把这个整齐的小脚本放在一起,测试用户的知识就在那里Star Trek知道脚本执行完美,但是当点击OK,取消或X时,问题仍然是正确答案。我知道我需要在某处设置vbCancel但不确定如何将其添加到脚本中。
'***********************************************************************
'Script Name: StarTrekQuiz.vbs
'Author: CharlesP
'Created: 03/31/15
'Description: This script creates a Star Trek Quiz game.
'***********************************************************************
'Perform script initialization activities
Option Explicit
Dim intPlayGame, strSplashImage, intNumberCorrect, strFederationRank
Dim objFsoObject, objQuestionDict, strAnswer, strQuestion
Const cTitlebarMsg = "The Star Trek Quiz Game"
Set objQuestionDict = CreateObject("Scripting.Dictionary")
objQuestionDict.Add "What was the Science Officer's name in the original Star Trek series?", "spock"
objQuestionDict.Add "What Star Trek villain appeared in both the original series and a Star Trek movie?", "khan"
objQuestionDict.Add "What was the numeric designation of Voyager's on-board Borg?", "7"
objQuestionDict.Add "Name the only Star Trek character to regularly appear on two series and at least two Star Trek movies?",
"worf"
objQuestionDict.Add "What is the last name of your favorite Captain?", "kirk picard sisco janeway archer"
objQuestionDict.Add "Who directed the first Star Trek movie?", "Robert Wise"
objQuestionDict.Add "In what Star Trek TOS episode did the tribbles FIRST appear?", "The Trouble With Tribbles"
objQuestionDict.Add "In what episode of Star Trek TNG did the Enterprise transport samples of Plasma Plague for medical
research?", "The Child"
objQuestionDict.Add "Who voices the computer in TNG?", "Majel Barrett"
objQuestionDict.Add "In TOS ~The Ultimate Computer~ who was the captain of the U.S.S. Excalibur?", "Captain Harris"
'Start the user's score at zero
intNumberCorrect = 0
'Display the splash screen and ask the user if he or she wants to play
strSplashImage = space(11) & "********" & vbCrLf & _
" ******************" & space(20) & "**************************" & _
space(15) & vbCrLf & "*" & space(30) & "*" & space(18) & _
"**" & space(39) & "*" & vbCrLf & " ******************" & _
space(20) & "*************************" & vbCrLf & space(31) & _
"******" & space(26) & "***" & vbCrLf & _
space(34) & "******" & space(22) & "***" & vbCrLf & _
space(37) & "******" & space(17) & "***" & vbCrLf & _
space(26) & " ****************************" & vbCrLf & _
space(26) & "*******************************" & vbCrLf & _
space(26) & "******************************" & vbCrLf & _
space(26) & " ****************************" & vbCrLf & vbCrLf & vbCrLf & _
"Would you like to boldly go where no one has gone before?"
'Ask the user to play
intPlayGame = MsgBox(strSplashImage, 36, cTitlebarMsg)
If intPlayGame = vbYes Then 'User elected to play the game
For Each strQuestion In objQuestionDict
strAnswer = InputBox(strQuestion, cTitlebarMsg)
If Trim(strAnswer) <> "" Then
If Instr(strAnswer, objQuestionDict(strQuestion)) Or Instr(objQuestionDict(strQuestion), strAnswer) Then
MsgBox "Correct"
intNumberCorrect = intNumberCorrect + 1
Else
MsgBox "Nice try. The answer I was looking for was " & objQuestionDict(strQuestion)
End If
End If
Next
Select Case intNumberCorrect
'User got 10 of 10 answers right
Case 10 : strFederationRank = "Fleet Admiral"
'User got 9 of 10 answers right
Case 9 : strFederationRank = "Admiral"
'User got 8 of 9 answers right
Case 8 : strFederationRank = "Vice Admiral"
'User got 7 of 8 answers right
Case 7 : strFederationRank = "Rear Admiral-Upper Hall"
'User got 6 of 7 answers right
Case 6 : strFederationRank = "Rear Admiral-Lower Hall"
'User got 5 of 5 answers right
Case 5 : strFederationRank = "Captain"
'User got 4 of 5 answers right
Case 4 : strFederationRank = "Commander"
'User got 3 of 5 answers right
Case 3 : strFederationRank = "Lieutenant-Commander"
'User got 2 of 5 answers right
Case 2 : strFederationRank = "Lieutenant"
'User got 1 of 5 answers right
Case 1 : strFederationRank = "Lieutenant Junior Grade"
'User did not get any answers right
Case 0 : strFederationRank = "Ensign"
End Select
MsgBox "You answered " & intNumberCorrect & " out of 10 correct." & _
vbCrLf & vbCrLf & "Your Star Fleet rank is : " & _
strFederationRank, , cTitlebarMsg
Else 'User doesn't want to play
MsgBox "Thank you for taking the Star Trek Quiz" & _
vbCrLf & vbCrLf & "Live long and prosper!", , cTitlebarMsg
WScript.Quit()
End If
非常感谢任何帮助,谢谢大家提前。
答案 0 :(得分:1)
与MsgBox
函数或Popup
方法不同,InputBox
函数没有vbCancel
选项。
如果用户点击确定或按 ENTER ,
InputBox
函数返回文本框中的内容。如果是用户 点击取消或×,该函数返回一个 零长度字符串(""
)。
严格来说,MSDN以上引文不正确。如果是用户
单击取消或×按钮或按 Esc 键,该函数返回Empty
值(与未初始化变量的情况相同)。
下一段代码可以提供帮助。
' Ask the user to play
intPlayGame = MsgBox(strSplashImage, vbYesNo + vbQuestion, cTitlebarMsg)
Dim WshShell 'prepare for POPUP method
Set WshShell = WScript.CreateObject("WScript.Shell")
If intPlayGame = vbYes Then 'User elected to play the game
For Each strQuestion In objQuestionDict
strAnswer = InputBox(strQuestion, cTitlebarMsg)
If VarType( strAnswer)=0 Then
intPlayGame = WshShell.Popup( _
"Cancel pressed or window closed. Continue?", 7, _
cTitlebarMsg, vbYesNo + vbQuestion)
If not intPlayGame = vbYes Then Wscript.Quit
strAnswer=""
End If
if strAnswer="" Then strAnswer="???"
If Instr(1, strAnswer, objQuestionDict(strQuestion), vbTextCompare) _
Or Instr(1, objQuestionDict(strQuestion), strAnswer, vbTextCompare) Then
MsgBox "Correct"
intNumberCorrect = intNumberCorrect + 1
Else
MsgBox strAnswer & " Nice try. The answer I was looking for was " & objQuestionDict(strQuestion)
End If
Next
Select Case intNumberCorrect
预定义的MsgBox / Popup常量
' Button Type
' vbOKOnly 0 Display OK button only.
' vbOKCancel 1 Display OK and Cancel buttons.
' vbAbortRetryIgnore 2 Display Abort, Retry, and Ignore buttons.
' vbYesNoCancel 3 Display Yes, No, and Cancel buttons.
' vbYesNo 4 Display Yes and No buttons.
' vbRetryCancel 5 Display Retry and Cancel buttons.
' Icon Type
' vbCritical 16 Display Critical Message icon.
' vbQuestion 32 Display Warning Query icon.
' vbExclamation 48 Display Warning Message icon.
' vbInformation 64 Display Information Message icon.
' Default Button: MsgBox Function only
' vbDefaultButton1 0 First button is the default.
' vbDefaultButton2 256 Second button is the default.
' vbDefaultButton3 512 Third button is the default.
' vbDefaultButton4 768 Fourth button is the default.
' Modality of the box: MsgBox Function only
' vbApplicationModal 0 Application modal. The user must respond to the message box before continuing work in the current application.
' vbSystemModal 4096 System modal. On Win16 systems, all applications are suspended until the user responds to the message box. On Win32 systems, this constant provides an application modal message box that always remains on top of any other programs you may have running.
' Return Value
' vbOK 1 OK
' vbCancel 2 Cancel
' vbAbort 3 Abort
' vbRetry 4 Retry
' vbIgnore 5 Ignore
' vbYes 6 Yes
' vbNo 7 No
' vbTrue -1 nSecondsToWait elapsed, the user did not click a button before
'
修改强>:
@ Ekkehard.Horner是正确的,如果点击取消或×以回复CStr(VarType(strAnswer)) & TypeName(strAnswer)
,0Empty
会返回InputBox
。我的资源:MSDN VBScript language reference。
但这只是故障查询器的狡辩,因为同一个MSDN:how expressions are compared:如果一个表达式是Empty
而另一个是字符串,那么使用零长度字符串执行字符串比较({{ 1}})作为""
表达式。
因此,无论Empty
变量
If strAnswer = "" ...
比较都会得到相同的结果
strAnswer
或""
。但是,我们可以测试Empty
并允许用户正确终止测验未完成(请参阅代码段中的更新,使用VarType(strAnswer)
方法)。
答案 1 :(得分:0)
单击“取消”将在“输入框”中返回空字符串。
因此,检查答案是否为&#34;&#34;。
For Each strQuestion In objQuestionDict
strAnswer = InputBox(strQuestion, cTitlebarMsg)
If Trim(strAnswer) <> "" Then
If Instr(strAnswer, objQuestionDict(strQuestion)) Or Instr(objQuestionDict(strQuestion), strAnswer) Then
MsgBox "Correct"
intNumberCorrect = intNumberCorrect + 1
Else
MsgBox "Nice try. The answer I was looking for was " & objQuestionDict(strQuestion)
End If
End If
Next