猿问

如何使程序连续运行?

我正在创建一个石头剪刀布游戏(我故意省略了代码的某些部分,只是为了让事情更容易理解),我希望能够在按下按钮一次后继续游戏。按照现在游戏的设置方式,一旦你按下按钮,游戏就会告诉你是赢了还是输了。此时,如果您尝试再次按下该按钮,则不会发生任何反应。我一直在尝试几种不同的方法,但我似乎找不到一种方法来做到这一点。如果有人可以帮助我做到这一点,以便在我再次单击按钮后,游戏能够继续与计算机一起选择新的选择,依此类推,将不胜感激。谢谢。


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

const computersChoice = choices[Math.floor(Math.random() * choices.length)];

const rockButton = document.getElementById("rockbtn");

const updateText = document.getElementById('textField')

let yourChoice

let status

let statusComp



//function for when user clicks rock button

function userRock() {

    rockButton.addEventListener ("click", function() {

        yourChoice = choices[0]  

        execute()

  });

}


function execute(){

    checker()

    computersTurn()

}


// checks to see if user made a choice

function checker(){

    if (yourChoice === choices[0]) {

        status ='rock'

    }

  }  


// computer chooses

function computersTurn() {

        statusComp = computersChoice

        //logs check to make sure the program is running correctly

        console.log(status)

        console.log(statusComp)

        if (status === statusComp) {

            updateText.innerText = 'It\s a tie'

        } else if (status === 'rock' && statusComp === 'paper'){

            updateText.innerText = 'You lose... Computer chose paper'

        } else if (status === 'rock' && statusComp === 'scissors'){

            updateText.innerText = 'You win... Computer chose scissors'

        }

  }



function startGame(){

    userRock()


}


startGame()


慕桂英546537
浏览 98回答 2
2回答

慕雪6442864

问题是你只得到一次电脑选择。您应该在每次单击时获得计算机选择。const choices = ["rock", "paper", "scissors"];let computersChoice ;const rockButton = document.getElementById("rockbtn");const updateText = document.getElementById('textField')let yourChoicelet statuslet statusComp//function for when user clicks rock buttonfunction userRock() {&nbsp; &nbsp; rockButton.addEventListener ("click", function() {&nbsp; &nbsp; &nbsp; &nbsp; yourChoice = choices[0];&nbsp; &nbsp; &nbsp; &nbsp; computersChoice = choices[Math.floor(Math.random() * choices.length)];&nbsp; &nbsp; &nbsp; &nbsp; execute()&nbsp; });}function execute(){&nbsp; &nbsp; checker()&nbsp; &nbsp; computersTurn()}// checks to see if user made a choicefunction checker(){&nbsp; &nbsp; if (yourChoice === choices[0]) {&nbsp; &nbsp; &nbsp; &nbsp; status ='rock'&nbsp; &nbsp; }&nbsp; }&nbsp;&nbsp;// computer choosesfunction computersTurn() {&nbsp; &nbsp; &nbsp; &nbsp; statusComp = computersChoice&nbsp; &nbsp; &nbsp; &nbsp; //logs check to make sure the program is running correctly&nbsp; &nbsp; &nbsp; &nbsp; console.log(status)&nbsp; &nbsp; &nbsp; &nbsp; console.log(statusComp)&nbsp; &nbsp; &nbsp; &nbsp; if (status === statusComp) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateText.innerText = 'It\s a tie'&nbsp; &nbsp; &nbsp; &nbsp; } else if (status === 'rock' && statusComp === 'paper'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateText.innerText = 'You lose... Computer chose paper'&nbsp; &nbsp; &nbsp; &nbsp; } else if (status === 'rock' && statusComp === 'scissors'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateText.innerText = 'You win... Computer chose scissors'&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; }function startGame(){&nbsp; &nbsp; userRock()}startGame()<button id="rockbtn">Play</button><span id="textField"/></span>

斯蒂芬大帝

这很简单。只需将创建计算机选项的一行移动到您的函数即可。这样,计算机就不会一直使用相同的结果。其他一切都可以保持不变,并且会起作用。computerTurns()const choices = ["rock", "paper", "scissors"];const rockButton = document.getElementById("rockbtn");const updateText = document.getElementById('textField')let computersChoice;let yourChoicelet statuslet statusComp//function for when user clicks rock buttonfunction userRock() {&nbsp; &nbsp; rockButton.addEventListener ("click", function() {&nbsp; &nbsp; &nbsp; &nbsp; yourChoice = choices[0]&nbsp;&nbsp;&nbsp; &nbsp; &nbsp; &nbsp; execute()&nbsp; });}function execute(){&nbsp; &nbsp; checker()&nbsp; &nbsp; computersTurn()}// checks to see if user made a choicefunction checker(){&nbsp; &nbsp; if (yourChoice === choices[0]) {&nbsp; &nbsp; &nbsp; &nbsp; status ='rock'&nbsp; &nbsp; }&nbsp; }&nbsp;&nbsp;// computer choosesfunction computersTurn() {&nbsp; &nbsp; &nbsp; &nbsp; computersChoice = choices[Math.floor(Math.random() * choices.length)];&nbsp; &nbsp; &nbsp; &nbsp; statusComp = computersChoice&nbsp; &nbsp; &nbsp; &nbsp; //logs check to make sure the program is running correctly&nbsp; &nbsp; &nbsp; &nbsp; console.log(status)&nbsp; &nbsp; &nbsp; &nbsp; console.log(statusComp)&nbsp; &nbsp; &nbsp; &nbsp; if (status === statusComp) {&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateText.innerText = 'It\s a tie'&nbsp; &nbsp; &nbsp; &nbsp; } else if (status === 'rock' && statusComp === 'paper'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateText.innerText = 'You lose... Computer chose paper'&nbsp; &nbsp; &nbsp; &nbsp; } else if (status === 'rock' && statusComp === 'scissors'){&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; updateText.innerText = 'You win... Computer chose scissors'&nbsp; &nbsp; &nbsp; &nbsp; }&nbsp; }function startGame(){&nbsp; &nbsp; userRock()}startGame()<button id="rockbtn">Rock</button><textarea id="textField"></textarea>
随时随地看视频慕课网APP

相关分类

JavaScript
我要回答