VBScript 猜一个数字 [英] VBScript Guess a number

查看:29
本文介绍了VBScript 猜一个数字的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我有点不知所措.我一直在为一个班级的猜数字"游戏编写这个脚本,到目前为止我已经成功了.现在分配要求当用户猜测太高或太低时,它应该为他们提供关于他们有多远的更好线索.

So I am at a bit of a loss. I have been working on this script for a "Guess a Number" game for a class and so far I have been successful. Now the assignment requires that when the user guesses either too high or too low it should give them better clues as to how far away they are.

例如,如果用户在 50 个号码之外,它应该通知他们他们很冷.如果它们相距 30 个号码,则它们很温暖,如果它们相距 10 个号码,它们就很热...

If, for example, the user is 50 numbers away it should notify them they are cold. If they are 30 numbers away they are warm, 10 numbers away they are hot...

我无法弄清楚那部分.

非常感谢任何帮助.

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 Then
        MsgBox "Your guess was too low. Try again", ,cGreetingMsg
        strOkToEnd = "no"
      End If

      'Test to see if the user's guess was too high
      If FormatNumber(intUserNumber) > intRandomNo Then
        MsgBox "Your guess was too high. Try again", ,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

推荐答案

您的问题可能与 S.O.无论如何我都会给你一个提示,因为我曾经在你的鞋子里...

Your question is probably off topic for S.O. I will give you a hint anyway because I have been in your shoes once...

这会敲响警钟吗?

coldThresHold = 20

If userNumber = intRandomNo + coldThresHold Then YouGuessed()
Else If userNumber > intRandomNo + coldThresHold Then YouAreCold()
Else If userNumber > intRandomNo Then YouAreHot()
Else If userNumber < intRandomNo - coldThresHold Then YouAreCold()
Else userNumber < intRandomNo Then YouAreHot()

这篇关于VBScript 猜一个数字的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆