我在这里完全失败了。整个周末我一直在靠墙打我的头,我不能为我的生活弄清楚如何使我的程序工作。这是一个大学课程作业,这是我有史以来第一次编程,所以这对我来说是一个真正的挑战。我用谷歌搜索直到我的手指流血(不是真的),我只是遇到了一些有用的信息。这是我的任务(如果我的格式错误,我深感抱歉):
I am at a complete loss here. I have been beating my head against a wall all weekend and I cannot for the life of me figure out how to make my program work. This is a college class assignment and it is my first programming assignment ever, so this is a real challenge for me. I have Googled until my fingers were bleeding (not really) and I have only come across marginally helpful information. Here is my assignment (I am deeply sorry if I get the formatting wrong):
'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猜一个数字和那个话虽如此,我还没有想到如何将代码实现到我自己的代码中。看起来它会起作用,但每当我尝试它时,VBScript会抛出大量预期的语句结束错误。我自己的代码在努力中几乎毫无价值,但这里是:
This is blowing my mind. The only thing that has been remotely helpful has been this link: VBScript Guess a number and with that being said, I have no earthly idea how to even implement that code into my own. It looks like it would work, but whenever I try it VBScript throws tons of "expected end of statement" errors. My own code is near worthless in the effort, but here it is:
'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非常感谢任何和所有帮助。谢谢。
Any and all help is greatly appreciated with this. Thank you.
推荐答案这里的主要问题是你的所有数字都是字符串。 FormatNumber函数接受一个数字并返回一个字符串,因此intRandomNo是一个字符串。 InputBox还返回一个字符串,意思是intUserNumber也是一个字符串。如果将这两个值保持为字符串,则无法计算差异。你要做的第一件事是将它们转换为整数。我不会为你写所有的代码,但我会给你一个提示。首先声明这个变量:
Your main problem here is that all your numbers are really strings. The FormatNumber function takes a number and returns a string so intRandomNo is a string. InputBox also returns a string meaning intUserNumber is also a string. It will not be possible to calculate the difference if you keep those two values as strings. The first thing you have to do is to convert them to integers. I will not write all the code for you but I will give you a hint. First declare this variable:
Dim Difference然后在你的循环中为它赋值:
Then assign it this value inside your loop:
Difference = CInt(intRandomNo) - CInt(intUserNumber)...并在嵌套if中使用该变量。之后应该很容易。
... and use that variable in your nested if. It should be easy after that.
更多推荐
VBScript数字猜测游戏
发布评论