gpt4 book ai didi

javascript - 为什么我的 "Rock Paper Scissors"程序无法运行并且不断使我的页面崩溃?

转载 作者:行者123 更新时间:2023-12-02 23:59:35 26 4
gpt4 key购买 nike

我的“剪刀石头布”作业也要求我使用非常具体的直径。它根本不起作用。该页面甚至不会加载,而不是出现任何记录的控制台。

我最初将控制台中的所有字符串作为在页面上弹出的警报,但这不起作用,所以我只是将其切换到控制台,希望如果我使用节点它会弹出,但这也没有不工作。我在播放器和计算机的胜利之间移动,进出功能,看看这是否会改变任何东西,但它没有。


var hands = ['Rock', 'Paper', 'Scissors'];

function getHand(){
return hands[parseInt(Math.random()*hands.length)%3];
}

var player1 = "Mike";
console.log("Hello " + player1);

var player2;
console.log("Player 2 is the computer!");

//var playWins = 0;
//var compWins = 0;

function playRound(){

if (hand == computer){
console.log("You guys tied!");

} else if (hand == "Scissors" && computer == "Rock"){
console.log(player1 + " has lost! Sadddd...");
compWins++;
} else if (hand == "Paper" && computer == "Rock"){
console.log(player1 + " has wonnnn!!! Yay! I guess...");
playWins++

} else if (hand == "Rock" && computer == "Scissors"){
console.log(player1 + " has wonnnn!!! Yay! I guess...");
playWins++;
} else if (hand == "Paper" && computer == "Scissors"){
console.log(player1 + + " has lost! Sadddd...");
compWins++;

} else if (hand == "Rock" && computer == "Paper"){
console.log(player1 + + " has lost! Sadddd...");
compWins++;
} else if (hand == "Paper" && computer == "Paper"){
console.log(player1 + " has wonnnn!!! Yay! I guess...");
playWins++;
} else {console.log("There seems to be a problem.");}
}


hand = [];
computer = [];


while (hand <= 3 || computer <= 3){
for (i = 0; i >=5; i++ ){
hand.push(getHand());
computer.push(getHand());

playRound();
}
}

if(playWins>compWins){
console.log(player1 + " Wins!");
} else {
console.log("Computer Wins!");
}

我希望它能告诉我谁赢得了每场比赛,然后谁在至少 5 场比赛后获胜。我看不到错误消息,因为该页面甚至无法完全加载。

最佳答案

你有一个无限循环。一些错误导致了这种情况的发生。首先,这些是数组:

while (hand <= 3 || computer <= 3)

所以我猜您想检查它们的长度?:

while (hand.length <= 3 || computer.length <= 3)

那么你这里也有一个逻辑错误:

for (i = 0; i >=5; i++ ){

i开头为 0 ,它立即>= 5 ,所以for永远不会进入循环,while循环只是无限重复。您可能想要<= :

for (i = 0; i <= 5; i++ ){

您还需要取消注释这些变量,以便定义变量,因为稍后您将使用这些变量:

var playWins = 0;
var compWins = 0;

关于javascript - 为什么我的 "Rock Paper Scissors"程序无法运行并且不断使我的页面崩溃?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58576023/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com