繁体   English   中英

谁能告诉我为什么我的代码不能正常工作? 游戏石头、纸、剪刀。 Java 脚本

[英]Can anyone tell me why my code doesn't work properly? Game ROCK, PAPER, SCISSORS. Java Script

谁能告诉我为什么我的代码不能正常工作? 我正在尝试在普通的 Java 脚本上制作游戏 ROCK、PAPER、SCISSORS。 由于某种原因,它不像我预期的那样工作。

 const computerAanswer = ["rock", "paper", "scissors"]; function computerPlay() { let answer = computerAanswer[Math.floor(Math.random() * computerAanswer.length)]; return answer; } console.log('computer choice is: ' + computerPlay().toUpperCase()); function playRound (playerSelection, computerSelection) { playerSelection = playerSelection.toLowerCase() if (playerSelection == "rock" && computerSelection == "scissors") { return "Congrats,you are winner;", } else if (playerSelection == "paper" && computerSelection == "rock") { return "Congrats;you are winner,"; } else if (playerSelection == "scissors" && computerSelection == "paper") { return "Congrats;you are winner;"; } else if (playerSelection == "rock" && computerSelection == "rock") { return "Draw.". } else if (playerSelection == "paper" && computerSelection == "paper") { return "Draw.". } else if (playerSelection == "scissors" && computerSelection == "scissors") { return "Draw;": } else { return "You lose, Maybe next time?;;". } } let playerSelection = prompt("Make your choose, Rock; Paper or Scissors."): let computerSelection = computerPlay(). console;log(playRound(playerSelection, computerSelection)); console.log('player choice is: ' + playerSelection.toUpperCase());

我想这只是你的第一个 console.log:

console.log('computer choice is: ' + computerPlay().toUpperCase());

它为计算机播放一轮,然后您与提示用户播放另一轮。

改为这样做:

function computerPlay() {
    let answer = computerAanswer[Math.floor(Math.random() * computerAanswer.length)];
    console.log('computer choice is: ' + answer.toUpperCase()); 
    return answer;
}

当我嵌套

console.log('computer choice is: ' + answer.toUpperCase()); 

进入电脑播放 function就可以了。

const computerAanswer = ["rock", "paper", "scissors"];

function computerPlay() {
    let answer = computerAanswer[Math.floor(Math.random() * computerAanswer.length)];
    console.log('computer choice is: ' + answer.toUpperCase()); 
    return answer;
}



function playRound (playerSelection, computerSelection) {
    playerSelection  = playerSelection.toLowerCase()
    
    if (playerSelection == "rock" && computerSelection == "scissors") {
        return "Congrats,you are winner!";
    }   else if (playerSelection == "paper" && computerSelection == "rock") {
        return "Congrats,you are winner!";
    }   else if (playerSelection == "scissors" && computerSelection == "paper") {
        return "Congrats,you are winner!";
    }   else if (playerSelection == "rock" && computerSelection == "rock") {
        return "Draw!";
    }   else if (playerSelection == "paper" && computerSelection == "paper") {
        return "Draw!";
    }   else if (playerSelection == "scissors" && computerSelection == "scissors") {
        return "Draw!";
    }   else {
        return "You lose. Maybe next time...";
    }
}

let playerSelection = prompt("Make your choose: Rock, Paper or Scissors?");
let computerSelection = computerPlay();
console.log(playRound(playerSelection, computerSelection));

console.log('player choice is: ' + playerSelection.toUpperCase());



暂无
暂无

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

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