2017-02-26 17 views
0

ここでは完全に迷っています。私は週末のうちに壁に向かって頭を打ち、私の人生は私のプログラムの仕事をどうやって作るかを考え出すことができません。これはカレッジクラスの課題であり、私の最初のプログラミング課題です。これは私にとって本当の課題です。私の指が出血するまで(本当はではない)私はグーグルで行きました。ここに私の割り当ては(私は、フォーマットが間違っを取得する場合、私は深く申し訳ありません)です: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 
    '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 Guess a numberであり、私は自分のコードにそのコードをどのように実装するのかについては考えていません。それはうまくいくように見えますが、私が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 

すべてのヘルプはこれで大いに感謝しています。ありがとうございました。

+0

コードのインデントを確認する必要があります。 –

答えて

2

ここでの主な問題は、すべての数字が実際に文字列であることです。 FormatNumber関数は数値を取り、文字列を返すので、intRandomNoは文字列です。 InputBoxはintUserNumberも文字列であることを意味する文字列を返します。これら2つの値を文字列として保持すると、その差を計算することはできません。最初に行う必要があるのは、それらを整数に変換することです。私はあなたのためのすべてのコードを書いていないが、私はあなたにヒントを与えるだろう。

​​

は、その後、あなたのループ内でそれにこの値を割り当てる:

Difference = CInt(intRandomNo) - CInt(intUserNumber) 

を...そして、あなたのネストされた場合には、その変数を使用して最初にこの変数を宣言します。それはその後簡単だろう。

-1
'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 
+0

書式設定で何か問題が発生しました... –