簡體   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