If statement VBScript数字猜谜游戏
我在这里完全不知所措。整个周末我都在碰壁,我一辈子都不知道如何让我的程序工作。这是一个大学课堂作业,也是我有史以来的第一个编程作业,所以这对我来说是一个真正的挑战。我一直在谷歌上搜索,直到我的手指流血不止,而且我只找到了一些有用的信息。这是我的作业,如果格式错误,我深表歉意:If statement VBScript数字猜谜游戏,if-statement,vbscript,numbers,logic,If Statement,Vbscript,Numbers,Logic,我在这里完全不知所措。整个周末我都在碰壁,我一辈子都不知道如何让我的程序工作。这是一个大学课堂作业,也是我有史以来的第一个编程作业,所以这对我来说是一个真正的挑战。我一直在谷歌上搜索,直到我的手指流血不止,而且我只找到了一些有用的信息。这是我的作业,如果格式错误,我深表歉意: 'Initialization Section Option Explicit Const cGreetingMsg = "Pick a number between 1 - 100" Dim intUserNumber,
'Initialization Section
Option Explicit
Const cGreetingMsg = "Pick a number between 1 - 100"
Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses
intNoGuesses = 0
'Main Processing Section
'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((100 * Rnd) + 1))
'Loop until either the user guesses correctly or the user clicks on Cancel
Do Until strOkToEnd = "yes"
'Prompt user to pick a number
intUserNumber = InputBox("Type your guess:",cGreetingMsg)
intNoGuesses = intNoGuesses + 1
'See if the user provided an answer
If Len(intUserNumber) <> 0 Then
'Make sure that the player typed a number
If IsNumeric(intUserNumber) = True Then
'Test to see if the user's guess was correct
If FormatNumber(intUserNumber) = intRandomNo Then
MsgBox "Congratulations! You guessed it. The number was " & _
intUserNumber & "." & vbCrLf & vbCrLf & "You guessed it " & _
"in " & intNoGuesses & " guesses.", ,cGreetingMsg
strOkToEnd = "yes"
End If
'Test to see if the user's guess was too low
'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is less than
'80,60,40,20,10 or farther from the correct guess, but still too low
If FormatNumber(intUserNumber) < intRandomNo Then
strOkToEnd = "no"
End If
'Test to see if the user's guess was too high
'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is more than
'80,60,40,20,10 or closer to the correct guess, but still too high
If FormatNumber(intUserNumber) > intRandomNo Then
strOkToEnd = "no"
End If
Else
MsgBox "Sorry. You did not enter a number. Try again.", , cGreetingMsg
End If
Else
MsgBox "You either failed to type a value or you clicked on Cancel. " & _
"Please play again soon!", , cGreetingMsg
strOkToEnd = "yes"
End If
Loop
这真让我大吃一惊。唯一有点帮助的就是这个链接:说到这里,我根本不知道如何在我自己的代码中实现这个代码。它看起来可以工作,但每当我尝试它时,VBScript就会抛出大量预期的语句结束错误。我自己的代码在这方面几乎毫无价值,但它是:
'Initialization Section
Option Explicit
Const cGreetingMsg = "Pick a number between 1 - 100"
Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses
intNoGuesses = 0
'Main Processing Section
'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((100 * Rnd) + 1))
'Loop until either the user guesses correctly or the user clicks on Cancel
Do Until strOkToEnd = "yes"
'Prompt user to pick a number
intUserNumber = InputBox("Type your guess:",cGreetingMsg)
intNoGuesses = intNoGuesses + 1
'See if the user provided an answer
If Len(intUserNumber) <> 0 Then
'Make sure that the player typed a number
If IsNumeric(intUserNumber) = True Then
'Test to see if the user's guess was correct
If FormatNumber(intUserNumber) = intRandomNo Then
MsgBox "Congratulations! You guessed it. The number was " & _
intUserNumber & "." & vbCrLf & vbCrLf & "You guessed it " & _
"in " & intNoGuesses & " guesses.", ,cGreetingMsg
strOkToEnd = "yes"
End If
'Test to see if the user's guess was too low
If FormatNumber(intUserNumber) < intRandomNo - 80 Then
MsgBox "Your guess was too low by 80. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo - 60 Then
MsgBox "Your guess was too low by 60. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo - 40 Then
MsgBox "Your guess was too low by 40. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo - 20 Then
MsgBox "Your guess was too low by 20. Try again", ,cGreetingMsg
Elseif FormatNumber(intUserNumber) < intRandomNo - 10 Then
MsgBox "Your guess was too low by 10. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo Then
MsgBox "Your guess was still too low. Try again, please", ,cGreetingMsg
strOkToEnd = "no"
End If
'Test to see if the user's guess was too high
If FormatNumber(intUserNumber) > intRandomNo - 80 Then
MsgBox "Your guess was too high by 80. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 60 Then
MsgBox "Your guess was too high by 60. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 40 Then
MsgBox "Your guess was too high by 40. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 20 Then
MsgBox "Your guess was too high by 20. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) > intRandomNo + 10 Then
MsgBox "Your guess was too high by 10. Try again", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) < intRandomNo Then
MsgBox "Your guess was still too low. Try again, please", ,cGreetingMsg
strOkToEnd = "no"
End If
Else
MsgBox "Sorry. You did not enter a number. Try again.", , cGreetingMsg
End If
Else
MsgBox "You either failed to type a value or you clicked on Cancel. " & _
"Please play again soon!", , cGreetingMsg
strOkToEnd = "yes"
End If
Loop
非常感谢您的帮助。谢谢。您的主要问题是,您所有的数字实际上都是字符串。FormatNumber函数接受一个数字并返回一个字符串,因此intRandomNo是一个字符串。InputBox还返回一个字符串,表示intUserNumber也是一个字符串。如果将这两个值保留为字符串,则无法计算差值。你要做的第一件事就是把它们转换成整数。我不会为你写所有的代码,但我会给你一个提示。首先声明此变量:
Dim Difference
然后在循环中为其指定此值:
Difference = CInt(intRandomNo) - CInt(intUserNumber)
。。。并在嵌套if中使用该变量。在那之后应该很容易。您肯定应该检查代码缩进。
'Initialization Section
Option Explicit
Const cGreetingMsg = "Pick a number between 1 - 100"
Dim intUserNumber, intRandomNo, strOkToEnd, intNoGuesses, stringToNumber
intNoGuesses = 0
'Main Processing Section
'Generate a random number
Randomize
intRandomNo = FormatNumber(Int((100 * Rnd) + 1))
'Loop until either the user guesses correctly or the user clicks on Cancel
Do Until strOkToEnd = "yes"
'Prompt user to pick a number
intUserNumber = InputBox("Type your guess:",cGreetingMsg)
intNoGuesses = intNoGuesses + 1
'See if the user provided an answer
If Len(intUserNumber) <> 0 Then
'Make sure that the player typed a number
If IsNumeric(intUserNumber) = True Then
'Test to see if the user's guess was correct
If FormatNumber(intUserNumber) = intRandomNo Then
MsgBox "Congratulations! You guessed it. The number was " & _
intUserNumber & "." & vbCrLf & vbCrLf & "You guessed it " & _
"in " & intNoGuesses & " guesses.", ,cGreetingMsg
strOkToEnd = "yes"
End If
'Test to see if the user's guess was too low
'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is less than
'80,60,40,20,10 or farther from the correct guess, but still too low
If FormatNumber(intUserNumber) < intRandomNo Then
MsgBox "The number you chose is too low", ,cGreetingMsg
strOkToEnd = "no"
End If
If FormatNumber(intRandomNo) - intUserNumber = 10 Then
MsgBox "Too low 10 off!", ,cGreetingMsg
ElseIf FormatNumber(intRandomNo) - intUserNumber = 20 Then
MsgBox "Too low 20 off!", ,cGreetingMsg
ElseIf FormatNumber(intRandomNo) - intUserNumber = 40 Then
MsgBox "Too low 40 off!", ,cGreetingMsg
ElseIf FormatNumber(intRandomNo) - intUserNumber = 60 Then
MsgBox "Too low 60 off!", ,cGreetingMsg
ElseIf FormatNumber(intRandomNo) - intUserNumber = 80 Then
MsgBox "Too low 80 off!", ,cGreetingMsg
strOkToEnd = "no"
End If
If FormatNumber(intUserNumber) > intRandomNo Then
MsgBox "The number you chose is too high!", ,cGreetingMsg
strOkToEnd = "no"
End If
'Test to see if the user's guess was too high
'Assignment 3: Add another If/ElseIf/Else Condition to check for if the user is more than
'80,60,40,20,10 or closer to the correct guess, but still too high
If FormatNumber(intUserNumber) - intRandomNo = 10 Then
MsgBox "Too high 10 off!", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) - intRandomNo = 20 Then
MsgBox "Too high 20 off!", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) - intRandomNo = 40 Then
MsgBox "Too high 40 off!", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) - intRandomNo = 60 Then
MsgBox "Too high 60 off!", ,cGreetingMsg
ElseIf FormatNumber(intUserNumber) - intRandomNo = 80 Then
MsgBox "Too high 80 off!", ,cGreetingMsg
strOkToEnd = "no"
End If
Else
MsgBox "Sorry. You did not enter a number. Try again.", , cGreetingMsg
End If
Else
MsgBox "You either failed to type a value or you clicked on Cancel. " & _
"Please play again soon!", , cGreetingMsg
strOkToEnd = "yes"
End If
Loop