亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

如何使程序連續運行?

如何使程序連續運行?

慕桂英546537 2022-09-02 20:57:29
我正在創建一個石頭剪刀布游戲(我故意省略了代碼的某些部分,只是為了讓事情更容易理解),我希望能夠在按下按鈕一次后繼續游戲。按照現在游戲的設置方式,一旦你按下按鈕,游戲就會告訴你是贏了還是輸了。此時,如果您嘗試再次按下該按鈕,則不會發生任何反應。我一直在嘗試幾種不同的方法,但我似乎找不到一種方法來做到這一點。如果有人可以幫助我做到這一點,以便在我再次單擊按鈕后,游戲能夠繼續與計算機一起選擇新的選擇,依此類推,將不勝感激。謝謝。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 yourChoicelet statuslet statusComp//function for when user clicks rock buttonfunction userRock() {    rockButton.addEventListener ("click", function() {        yourChoice = choices[0]          execute()  });}function execute(){    checker()    computersTurn()}// checks to see if user made a choicefunction checker(){    if (yourChoice === choices[0]) {        status ='rock'    }  }  // computer choosesfunction 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()
查看完整描述

2 回答

?
慕雪6442864

TA貢獻1812條經驗 獲得超5個贊

問題是你只得到一次電腦選擇。您應該在每次單擊時獲得計算機選擇。


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

let computersChoice ;

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];

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

        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()

<button id="rockbtn">Play</button>

<span id="textField"/></span>


查看完整回答
反對 回復 2022-09-02
?
斯蒂芬大帝

TA貢獻1827條經驗 獲得超8個贊

這很簡單。只需將創建計算機選項的一行移動到您的函數即可。這樣,計算機就不會一直使用相同的結果。其他一切都可以保持不變,并且會起作用。computerTurns()


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

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

const updateText = document.getElementById('textField')

let computersChoice;

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() {

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

        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()

<button id="rockbtn">Rock</button>

<textarea id="textField"></textarea>


查看完整回答
反對 回復 2022-09-02
  • 2 回答
  • 0 關注
  • 96 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號