该项目是关于制作一个岩石,剪刀游戏使用功能和循环等。
下面的代码是该项目的外观。
的赢家!
从那时起就一直在做这件事,不知道为什么不起作用
函数getComputerChoice() {
让价值=“岩石”,“纸”,“剪刀”;
返回valuesMath.floor(Math.random() * values.length);
}
函数getPlayerChoice() {
设getChoice = "rock";
设值= getChoice.trim();
设lowCase = value.toLowerCase();
设大写= lowCase.charAt(0).toUpperCase + lowCase.slice(1);
while (.includes(大写)){
值= getChoice.trim();
lowCase = value.toLowerCase();
大写= lowCase.charAt(0).toUpperCase + lowCase.slice(1);
}
回报资本化;
}
函数playRound(playerSelection,computerSelection) {
让游戏= "";
如果(
(playerSelection == "rock“&& computerSelection ==”纸张“)
(playerSelection ==“纸张”&& computerSelection ==“剪刀”)
(playerSelection ==“剪刀”& computerSelection == "rock")
){
返回(游戏=
“玩家输了!!”+ computerSelection +“搏击”+ playerSelection);
}如果.
(playerSelection ==“纸张”&& computerSelection == "rock")
(playerSelection ==“剪刀”& computerSelection ==“纸张”)
(playerSelection == "rock“&& computerSelection ==”剪刀“)
){
返回(游戏=
“玩家获胜!!”+ playerSelection +“殴打”+ computerSelection);
} else (playerSelection == computerSelection) {
返回(游戏=
“这是一个领带噪音”+ playerSelection +“v”+ computerSelection);
}否则{
回报(游戏=“欣快”);
}
}
函数对策(){
设playerScores = 0;
设computerScores = 0;
设computerSelection = "";
设playerSelection = "";
computerSelection = getComputerChoice();
playerSelection = getPlayerChoice();
for (设i= 0;i< 5;i++) {
如果(
playRound(
"player loses!! " + computerSelection + " beats " + playerSelection)
){
computerScores += 1;
console.log(
"you lost this round!! boo hoo!! scores are " + computerScores + " v " + playerScores);
}如果.
playRound(
"player Wins!! " + playerSelection + " beats " + computerSelection)
){
playerScores += 1;
console.log(
"you Won this round!! hurray!! scores are " + computerScores + " v " + playerScores);
}
}
if (playerScores > computerScores) {
Console.log(“恭喜你赢了这一轮”);
} else if (playerScores < computerScores) {
Console.log(“对不起你输了这一轮”);
}否则{
Console.log(“有问题”);
}
}
游戏();
发布于 2022-10-12 17:50:08
playRound()似乎没有返回任何内容:
function playRound(playerSelection, computerSelection) {
let games = '';
if (
(playerSelection == "rock" && computerSelection == "paper") ||
(playerSelection == "paper" && computerSelection == "scissors") ||
(playerSelection == "scissors" && computerSelection == "rock")
) {
games = "player loses!! " + computerSelection + " beats " + playerSelection;
// ADD RETURN HERE
} else if (
(playerSelection == "paper" && computerSelection == "rock") ||
(playerSelection == "scissors" && computerSelection == "paper") ||
(playerSelection == "rock" && computerSelection == "scissors")
) {
games = "player Wins!! " + playerSelection + " beats " + computerSelection;
// ADD RETURN HERE
} else if (playerSelection == computerSelection) {
games = "it a tie noice " + playerSelection + " v " + computerSelection;
// ADD RETURN HERE
} else {
games = "euphoria";
// ADD RETURN HERE
}
}在()函数定义中,您要检查是否(playRound())。playRound没有返回任何内容,因此这将被解释为一个空函数,因此,if将始终计算为false。
https://stackoverflow.com/questions/74046000
复制相似问题