简体   繁体   English

While循环忽略初始条件,浏览器崩溃

[英]While loop ignores initial condition and the browser crashes

So long story short - I'm trying to build a simple tennis match simulation (code below). 长话短说-我正在尝试构建一个简单的网球比赛模拟(下面的代码)。 Unfortunately, something is wrong with my code, because the while loop I created ignores the condition put in brackets and starts to make infinite number of itinerations (browser crashes). 不幸的是,我的代码出了点问题,因为我创建的while循环会忽略放在方括号中的条件,并开始进行无数次迭代(浏览器崩溃)。 Could you please have a look at my code and tell me where the error lies? 您能否看一下我的代码,并告诉我错误在哪里?

var gamesPlayerOne = Math.floor(Math.random() * 8);
var gamesPlayerTwo = Math.floor(Math.random() * 8);
var tiebreak = Math.floor(Math.random() * 10);
var setsPlayerOne = 0;
var setsPlayerTwo = 0;
var scoreline = [];

function playTheGame(g1, g2) {

    while (setsPlayerOne < 2 && setsPlayerTwo < 2) {

        if (g1 === 6 && g2 < 5) {

            var result = g1.toString() + ":" + g2.toString();
            setsPlayerOne += 1;
            scoreline.push(result);

        } else if (g1 < 5 && g2 === 6) {

            var result = g1.toString() + ":" + g2.toString();
            setsPlayerTwo += 1;
            scoreline.push(result);

        } else if (g1 === 6 && g2 === 7) {

            var result = g1.toString() + ":" + g2.toString() + "(" + tiebreak + ")";
            setsPlayerTwo += 1;
            scoreline.push(result);

        } else if (g1 === 7 && g2 === 6) {

            var result = g1.toString() + ":" + g2.toString() + "(" + tiebreak + ")";
            setsPlayerTwo += 1;
            scoreline.push(result);

        }
    }
}

playTheGame(gamesPlayerOne,gamesPlayerTwo);
console.log(scoreline);

For example... you didn't specify any condition to increase setsPlayer[One/Two] when g1 is equals to 0 and g2 is equals to 7. 例如,当g1等于0且g2等于7时,您没有指定任何条件来增加setsPlayer [One / Two]。

So you should add some condition to check it. 因此,您应该添加一些条件进行检查。

If the random numbers that you pass into your function don't match any of the if or else if conditions then none of your variables is ever updated so the while loop's condition remains true forever. 如果传递给函数的随机数与任何ifelse if条件都不匹配,则不会更新任何变量,因此while循环的条件将永远保持为真。

If you are trying to simulate the entire tennis match, it would make more sense not to pass the function any arguments, and instead on each iteration of the while loop decide randomly which player just won the current game and then test if either player has won a set yet, perhaps something like this: 如果您要模拟整个网球比赛,则最好不要将任何参数传递给函数,而是在while循环的每次迭代中随机确定哪个球员刚刚赢得了当前比赛,然后测试其中一位球员是否赢得了比赛。一套,也许像这样:

 function playTheGame() { var g1 = 0; var g2 = 0; var setsPlayerOne = 0; var setsPlayerTwo = 0; var scoreline = []; while (setsPlayerOne < 2 && setsPlayerTwo < 2) { // determine a random winner for the current game if (Math.random() < 0.5) g1++; else g2++; // has one of the players just won a set? if (g1 >= 6 && g2 < g1 - 1) { var result = g1 + ":" + g2; setsPlayerOne += 1; scoreline.push(result); g1 = g2 = 0; } else if (g1 < g2 - 1 && g2 >= 6) { var result = g1 + ":" + g2; setsPlayerTwo += 1; scoreline.push(result); g1 = g2 = 0; } } return scoreline; } console.log(playTheGame()); 

Note that you don't need to call .toString() on g1 and g2 , because concatenating them with the string ":" implicitly converts the numbers to strings. 请注意,您不需要在g1g2上调用.toString() ,因为将它们与字符串":"串联会隐式将数字转换为字符串。

You could extend this to make one or the other player more likely to win (simulating different skill levels) by changing if (Math.random() < 0.5) to use a variable instead of hardcoding 0.5 . 您可以通过将if (Math.random() < 0.5)改为使用变量而不是硬编码0.5来扩展此范围,以使一个或另一个玩家更有可能获胜(模拟不同的技能水平)。

PS I couldn't be bothered to look up the rules for tennis to confirm how you win a set, but my vague recollection is that you have to get at least 6 games and be at least two games ahead of the other player, so that's what the code I've shown tries to implement... 附言:我无须费心查找网球规则以确认您如何赢得比赛,但我模糊的回忆是,您必须至少获得6场比赛, 并且至少领先另一位球员2场比赛,所以我展示的代码试图实现什么...

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM