JavaScript 中的无限循环 - while 循环
Infinite Loop in JavaScript - While Loop
有谁知道为什么这可能会导致无限循环。我只是不明白为什么!
我觉得问题可能出在我的 while 循环中播放到五个。
while 循环应该在 5 处停止,当玩家或计算机达到此值时,游戏应该停止。
这是石头,纸,剪刀。每场比赛的结果是玩家获胜,计算机获胜或平局。
一旦任何玩家达到 5 场胜利,游戏就应该结束。
问题是游戏没有按预期结束!
function getInput() {
console.log("Please choose either 'rock', 'paper', or 'scissors'.");
return prompt("Please choose either 'rock', 'paper', or 'scissors'");
}
function getPlayerMove() {
return getInput();
}
function randomPlay() {
var randomNumber = Math.random();
if (randomNumber < 0.33) {
return "rock";
}
else if (randomNumber < 0.66) {
return "paper";
}
else {
return "scissors";
}
}
function getComputerMove() {
return randomPlay();
}
function getWinner(playerMove,computerMove) {
var winner;
if (playerMove === 'rock' && computerMove === 'scissors') {
winner = 'Player';
}
else if (playerMove === 'scissors' && computerMove === 'paper') {
winner='Player';
}
else if (playerMove === 'paper' && computerMove === 'rock') {
winner='Player';
}
else if (playerMove === 'paper' && computerMove === 'scissors') {
winner='Computer';
}
else if (playerMove === 'rock' && computerMove === 'paper') {
winner='Computer';
}
else if (playerMove === 'scissors' && computerMove === 'rock') {
winner = 'Computer';
}
else {
winner = "tie";
}
return winner;
}
// A big limitation of this game is the user is only allowed to choose once! To allow more choices you'd need to design the program differently
function playToFive() {
var playerWins = 0;
var computerWins = 0;
var playerMove = getPlayerMove();
while ((playerWins <= 5) || (computerWins <= 5)) {
var computerMove = getComputerMove();
var winner = getWinner(getPlayerMove, getComputerMove);
console.log('The player has chosen ' + playerMove + '. The computer has chosen ' + computerMove);
if (winner === "player") {
playerWins += 1;
}
else if (winner === "computer") {
computerWins += 1;
}
if ((playerWins = 5) || (computerWins = 5)) {
console.log("The game is over! " + "The " + winner + " has taken out the game!");
console.log("The final score was. " + playerWins + " to " + computerWins);
}
else {
console.log("The " + winner + ' takes the round. It is now ' + playerWins + ' to ' + computerWins);
}
}
}
playToFive ();
问题是这一行
var winner = getWinner(getPlayerMove, getComputerMove);
您正在将函数引用传递给 getWinner(( 方法
var winner = getWinner(playerMove , computerMove );
这意味着您稍后需要再次获取移动,因此请将方法更改为(代码中修复了多个问题(
function playToFive()
{
var playerWins = 0;
var computerWins = 0;
while ((playerWins <= 5) && (computerWins <= 5)) //this line has && instead of ||
{
var computerMove = getComputerMove();
var playerMove = getPlayerMove(); //this line is now inside while loop
var winner = getWinner( playerMove , computerMove );
console.log('The player has chosen ' + playerMove + '. The computer has chosen ' + computerMove);
if (winner === "Player") { //P caps
playerWins += 1;
}
else if (winner === "Computer") { //C caps
computerWins += 1;
}
if ((playerWins == 5) || (computerWins == 5)) { //= is replaced by ==
console.log("The game is over! " + "The " + winner + " has taken out the game!");
console.log("The final score was. " + playerWins + " to " + computerWins);
}
else {
console.log("The " + winner + ' takes the round. It is now ' + playerWins + ' to ' + computerWins);
}
}
}
为了使其相应地运行:
if ((playerWins = 5) || (computerWins = 5)) {}
您需要使用 ==
运算符,因为只有一个=
用于值分配。
if ((playerWins == 5) || (computerWins == 5)) {}
首先
,在while循环中,你也需要更改它一个&&语句,否则如果计算机是8,玩家是3,循环将继续,直到两个玩家都是>5。
我认为首先你应该了解(==(和(===(之间的区别。
使用此代码(条件代码而不是赋值(
if ((playerWins == 5) || (computerWins == 5)) { //use logical Operator not assignment Oprtr.
console.log("The game is over! " + "The " + winner + " has taken out the game!");
console.log("The final score was. " + playerWins + " to " + computerWins);
}
而不是##
if ((playerWins = 5) || (computerWins = 5)) { // Error Code.
console.log("The game is over! " + "The " + winner + " has taken out the game!");
console.log("The final score was. " + playerWins + " to " + computerWins);
}
问题是这条线
您正在将函数引用传递给 getWinner(( 方法
使用这个
var winner = getWinner(playerMove , computerMove );
而不是
var winner = getWinner(getPlayerMove, getComputerMove);
对于此功能...
function getWinner(playerMove,computerMove) {
var winner;
if (playerMove === 'rock' && computerMove === 'scissors') {
winner = 'Player';
}
else if (playerMove === 'scissors' && computerMove === 'paper') {
winner='Player';
}
else if (playerMove === 'paper' && computerMove === 'rock') {
winner='Player';
}
else if (playerMove === 'paper' && computerMove === 'scissors') {
winner='Computer';
}
else if (playerMove === 'rock' && computerMove === 'paper') {
winner='Computer';
}
else if (playerMove === 'scissors' && computerMove === 'rock') {
winner = 'Computer';
}
else {
winner = "tie";
}
return winner;
}
每次此方法
tie
设置为获胜者。
但是你应该知道(==) equalto
和(===) equal value and equal type
之间的区别
谢谢大家。有不少问题。
干杯伙计们。绝对需要&&而不是||。 的 ||导致游戏继续甚至
var winner = getWinner(getPlayerMove, getComputerMove( 也是必需的。这导致了无限循环。
相关文章:
- 基于两个条件退出While循环
- 希望日期开始结束于while循环中的一个房间id的一个数组
- 为什么我的While循环不起作用
- 使用while循环+break是一种可接受的方式;goto;手术的结束
- 如何将while循环中生成的值存储到变量中,以便以后在JQuery中使用
- jquery,while循环在后台运行,同时while循环
- jquery悬停和while循环
- Javascript:用while循环在给定的count参数中重复一个字符串
- 如何修复 while 循环
- Jquery扩展点的宽度,同时通过while循环递增
- 斐波那契序列Javascript做while循环
- 为什么这个while循环不能使用AND逻辑运算符
- JavaScript while循环没有'不能在有条件的情况下工作
- While循环在JavaScript中永远运行
- 逻辑(JSint友好)等效于while循环中的此赋值
- Javascript:用if语句运行while循环,用子句随机生成内容
- 如何使javascript在SQL while循环中回显所有数据
- Javascript 'while' 循环,如果条件为 false,如何停止迭代
- 在 while 循环中选择与整数值对应的数组
- 我怎样才能让 while 循环等到用户在图片上向右滑动