在我的猜数游戏中遇到while循环问题

时间:2015-11-21 05:52:28

标签: javascript html loops for-loop while-loop

所以,我在开始一段时间或者循环创建一个转弯计数时遇到了麻烦,从6开始,允许猜测该数字的用户在他输了之前只有6回合并给出一条消息说他输了与应该被猜到的数字。每次他向文本框输入一个猜测时,转弯计数将逐一减少到0。我很困惑我将在哪里创建while循环,以及我将在何处放置while循环。有多种方法可以创建此操作吗?

谢谢!

    //creating the guessing game function using if and else if statements
var guessGame = function(){
    var guess = document.getElementById("guessTextbox").value;
    var guessOutput = document.getElementById("gameStatus");

    //if guessed number is equal to the random number, winning display message is shown
    if (guess == guessNum){
        guessOutput.value = guessOutput.value + "You've guessed correctly! The correct number was: " + guessNum;
    }
    //else if statement for guesses not in the 1-100 range displaying error/incorrect message
    else if (guess > 100 || guess < 1){
        guessOutput.value = guessOutput.value + "Incorrect number range!";
    }
    else if (guess > guessNum){
        guessOutput.value = guessOutput.value + "The guess is too high!";
    }
    else if (guess < guessNum){
        guessOutput.value = guessOutput.value + "The guess is too low!";
    }
    //else if guess is NOT a number, displays message
    else if (isNaN(guess)){
        guessOutput.value = guessOutput.value + "Input value is not a number!";
    }
};

3 个答案:

答案 0 :(得分:1)

这是实现这一目标的一种方式。我留下了一些评论来帮助您浏览。如果我能澄清任何事情,请告诉我。

// When initialized, guessGame returns a function that allows
// 6 guesses and then reveals the random number.
function guessGame() {

    // Start with 6 turns.
    this.turns = 6;

    // Generate random number guessNum s.t. 1 < guessNum < 100
    this.guessNum = Math.floor(Math.random() * (100 - 2)) + 2;

    return function() {
      var guessOutput = document.getElementById("gameStatus");

      // If no turns left, output the message and return.
      if (this.turns === 0) {
        guessOutput.value = guessOutput.value + "You've ran out of turns! The correct number was: " + this.guessNum;
        return;
      }

      // Otherwise, match the next guess against guessNum.
      var guess = parseInt(document.getElementById("guessTextbox").value, 10);

      // If equal, display the winning message
      if (guess === this.guessNum){
          guessOutput.value = guessOutput.value + "You've guessed correctly! The correct number was: " + this.guessNum;
      }
      // If not in range, display error message
      // Advice: might want to move this before the equality check.
      else if (guess > 100 || guess < 1){
          guessOutput.value = guessOutput.value + "Incorrect number range!";
      }
      // If greater than, ...
      else if (guess > this.guessNum){
          guessOutput.value = guessOutput.value + "The guess is too high!";
      }
      // If less than, ...
      else if (guess < this.guessNum){
          guessOutput.value = guessOutput.value + "The guess is too low!";
          }
      // If not a number, display error.
      // Advice: might want to move this to the top of the checks.
      else if (isNaN(guess)){
          guessOutput.value = guessOutput.value + "Input value is not a number!";
      }

      // Decrement the number of turns.
      this.turns -= 1;

    // This helps the embedded function locate the random number.
    }.bind(this);
}

<强>使用。

// Initialize the game
// (Suppose the random number is 30)
var game = new guessGame();

// Enter 21 in guessTextbox
game(); // => "The guess is too low!"

// Enter 22 in guessTextbox
game(); // => "The guess is too low!"

// Enter 23 in guessTextbox
game(); // => "The guess is too low!"

// Enter 24 in guessTextbox
game(); // => "The guess is too low!"

// Enter 50 in guessTextbox
game(); // => "The guess is too high!"

// Enter 49 in guessTextbox
game(); // => "The guess is too high!"

// Enter 48 in guessTextbox
game(); // => "You've ran out of turns! The correct number was: 30"

// Enter 47 in guessTextbox
game();  // => "You've ran out of turns! The correct number was: 30"

你明白了。

答案 1 :(得分:0)

可能你应该把它放在你调用函数的地方,并降低每次调用的猜测。并把你猜测的信息放在周期中。但我不知道你打算把它叫做什么。如果您在点击事件中调用它,则不需要循环。只需输入一个var猜测= 5;例如在页面加载时减少它在你的函数中。然后检查函数末尾是否为0。

答案 2 :(得分:0)

在这种情况下,while循环充当游戏循环。非零数字会评估truthy,因此while循环将一直运行,直到guessesRemaining等于falsy(在这种情况下为0)。我没有惩罚玩家猜测超出范围和非整数猜测,但如果这是你想要的东西,这是一个很容易的改变。为了简单起见,我在这个例子中为用户输入了一个prompt()。作为旁注,您通常会在测试正确答案之前检查有效值FIRST(即NaN,null等)。您可能还想在用户输入上使用parseInt()。

        //creating the guessing game function using if and else if statements
function guessGame(){
    var guessesRemaining = 6,
        guessNum = Math.floor(Math.random() * 100) + 1;
        guessOutput = document.getElementById("gameStatus");

    while(guessesRemaining){ // non-zero numbers are truthy
        var guess = prompt();
        //if guessed number is equal to the random number, winning display message is shown
        if (guess == guessNum){
            guessOutput.value = guessOutput.value + " You've guessed correctly! The correct number was: " + guessNum;
            guessesRemaining = 0; // No more guesses needed!!!
        }
        else if (isNaN(guess)){
            guessOutput.value = guessOutput.value + " Input value is not a number!";
            // decriment here if penalty for invalid guess is incurred
        }
        //else if statement for guesses not in the 1-100 range displaying error/incorrect message
        else if (guess > 100 || guess < 1){
            guessOutput.value = guessOutput.value + " Incorrect number range!";
            // decriment here if penalty for invalid guess is incurred
        }
        else if (guess > guessNum){
            guessOutput.value = guessOutput.value + " The guess is too high!";
            guessesRemaining--; // decriment remaining guesses
        }
        else if (guess < guessNum){
            guessOutput.value = guessOutput.value + " The guess is too low!";
            guessesRemaining--; // decriment remaining guesses
        }
    }
};

如果您需要明确任何事情,请告诉我。