努力用于循环重复功能5次 我目前正在构建一个程序,该程序允许用户通过控制台日志对计算机播放岩纸剪辑。目标是让游戏在五轮比赛后终止。 我能够...

问题描述 投票:0回答:1

getHumanChoice()



    
	
您已经定义了该函数

playGame()

,但是您从未称呼它。因此,为什么不进行用户输入或显示任何控制台消息。

此外,为了使内容更具可读性,请单独定义所有功能,不要在其他功能中定义它们,尤其是在这种情况下有三个功能时。这是不好的,因为每次调用

function playGame() { var humanScore = 0; var computerScore = 0; for (let i = 0; i < 5; i++) { function getComputerChoice() { let result = Math.ceil(Math.random() * 3); if (result === 1) { return "rock"; } else if (result === 2) { return "paper"; } else { return "scissors" } } const computerChoice = getComputerChoice(); function getHumanChoice() { let humanResult = prompt("Let's play Rock, Paper, Scissors! Enter your choice:"); if (humanResult.toLowerCase() === "rock") { return "rock"; } else if (humanResult.toLowerCase() === "paper") { return "paper"; } else if (humanResult.toLowerCase() === "scissors") { return "scissors"; } else { return "to not follow the rules"; } }; const humanChoice = getHumanChoice(); function playRound(humanChoice, computerChoice) { if (humanChoice === "rock" && computerChoice === "paper") { computerScore++; console.log(`Paper beats rock! AI wins! AI:${computerScore} Human:${humanScore}`); } else if (humanChoice === "rock" && computerChoice === "scissors") { humanScore++; console.log(`Rock beats scissors! You win! AI:${computerScore} Human:${humanScore}`); } else if (humanChoice == "rock" && computerChoice === "rock") { console.log("Y'all both picked rock! Let's try again!"); } else if (humanChoice === "paper" && computerChoice === "scissors") { computerScore++; console.log(`Scissors beats paper! AI wins! AI:${computerScore} Human:${humanScore}`); } else if (humanChoice === "paper" && computerChoice === "rock") { humanScore++; console.log(`Paper beats rock! You win! AI:${computerScore} Human:${humanScore}`); } else if (humanChoice === "paper" && computerChoice === "paper") { console.log("Y'all both picked paper! Let's try again!"); } else if (humanChoice === "scissors" && computerChoice === "scissors") { console.log("Y'all both picked scissors! Let's try again!"); } else if (humanChoice === "scissors" && computerChoice === "rock") { computerScore++; console.log(`Rock beats scissors! AI wins! AI:${computerScore} Human:${humanScore}`); } else if (humanChoice === "scissors" && computerChoice === "paper") { humanScore++; console.log(`Scissors beats paper! You win! AI:${computerScore} Human:${humanScore}`); } else { console.log("Try again! That's not a choice."); } } } }; playGame();

都会重新创建功能。将功能移动到
playRound()
之外也需要一些代码更改,因为您的一个功能
playGame()
依赖于变量
playGame()

playGame()

。解决方案是添加其他参数以跟踪分数。
javascript function loops console
1个回答
0
投票

最新问题
© www.soinside.com 2019 - 2025. All rights reserved.