第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號(hào)安全,請(qǐng)及時(shí)綁定郵箱和手機(jī)立即綁定
已解決430363個(gè)問(wèn)題,去搜搜看,總會(huì)有你想問(wèn)的

我的事件監(jiān)聽(tīng)器中只有部分 if-else 語(yǔ)句有效

我的事件監(jiān)聽(tīng)器中只有部分 if-else 語(yǔ)句有效

皈依舞 2024-01-18 20:34:57
代碼新手在這里。所以我正在嘗試對(duì)我的石頭剪刀布游戲進(jìn)行一些改變。我希望捕獲分?jǐn)?shù)并在頁(yè)面上顯示結(jié)果文本。每次單擊按鈕時(shí)都會(huì)顯示文本,但一旦我添加了 +1 增量和在屏幕上實(shí)時(shí)顯示分?jǐn)?shù)的代碼,文本就不會(huì)顯示。我只是首先在“rock”事件偵聽(tīng)器上測(cè)試它,以確保它有效。//Selects the classes .rock .paper .scissorsconst rock = document.querySelector(".rock")const paper = document.querySelector(".paper")const scissors = document.querySelector(".scissors")//new div under resultsconst results = document.getElementById("results");//new itemslet itemRock = document.createElement('h3');let itemPaper = document.createElement('h3');let itemScissors = document.createElement('h3');    //variable for computerPlay() function to be printed to #resultsitemRock.textContent = 'You have chosen rock!';itemPaper.textContent = 'You have chosen paper!';itemScissors.textContent = 'You have chosen scissors!';//Player + computer score for count purposeslet playerScore = 0;let computerScore = 0;rock.addEventListener('click', function() {    computerPlay();    if(computerPlay() === 'paper') {        itemRock.textContent = 'You have chosen rock and the computer has chosen paper.' + 'You lose, paper beats rock! Try again';        computerScore += 1;         document.getElementById("computer-score").innerText = computerScore;    } else if (computerPlay() === 'rock') {        itemRock.textContent = 'You have chosen rock and the computer has chosen rock.' +  'Its a draw, try again';    } else {        itemRock.textContent = 'You have chosen rock and the computer has chosen         scissors. ' + 'You win! Rock beats scissors.';        playerScore += 1;        document.getElementById("player-score").innerText = playerScore;    }    return 'rock';  });HTML 代碼:<div class="choice"><button type="button" class="rock">Rock</button><button type="button" class="paper">Paper</button><button type="button" class="scissors">Scissors</button></div>    <div id="results"></div><p>Player Score: <a id="player-score">0</a></p><p>Computer Score: <a id="computer-score">0</a></p>任何和所有的幫助將不勝感激!
查看完整描述

2 回答

?
慕婉清6462132

TA貢獻(xiàn)1804條經(jīng)驗(yàn) 獲得超2個(gè)贊

我看到您每次執(zhí)行 if/else 語(yǔ)句時(shí)都會(huì)調(diào)用 ComputerPlay() 函數(shù)。假設(shè)computerPlay() 隨機(jī)返回石頭、布或剪刀,這將返回不同的值。您可以做的一件簡(jiǎn)單的事情是將返回值存儲(chǔ)在像這樣的變量中


let computersHand = computerPlay();

然后在執(zhí)行 if/else 語(yǔ)句時(shí)使用此變量


if (computersHand === 'rock') {

? ? // ...

} else if (computersHand === 'paper') {

? ? // ...

} else {

? ? // ...

}

如果您出于某種原因不想將返回值存儲(chǔ)到變量中,可以使用 switch 語(yǔ)句


switch (computerPlay()) {

? ?case 'rock':

? ? ? ?// ...

? ? ? ?break;

? ?case 'paper':

? ? ? ?// ...

? ? ? ?break;

? ?case 'scissors':

? ? ? ?// ...

? ? ? ?break;

}


查看完整回答
反對(duì) 回復(fù) 2024-01-18
?
呼啦一陣風(fēng)

TA貢獻(xiàn)1802條經(jīng)驗(yàn) 獲得超6個(gè)贊

您應(yīng)該調(diào)用該computerPlay函數(shù)一次


rock.addEventListener('click', function() {

    

    const result = computerPlay();

    

    if (result) {

         itemRock.textContent = 'You have chosen rock and the computer has chosen paper. 

         ' + 'You lose, paper beats rock! Try again';

         computerScore += 1;

         document.getElementById("computer-score").innerText = computerScore;

    } else if(result) {

         itemRock.textContent = 'You have chosen rock and the computer has chosen rock. 

         ' +  'Its a draw, try again';

    } else {

         itemRock.textContent = 'You have chosen rock and the computer has chosen 

         scissors. ' + 'You win! Rock beats scissors.';

         playerScore += 1;

         document.getElementById("player-score").innerText = playerScore;

    }

  

    return 'rock';  

})

computerPlay像這樣調(diào)用函數(shù)


computerPlay();

if (computerPlay() === 'paper')

沒(méi)有意義,因?yàn)槟惴Q它為兩次(計(jì)算機(jī)一鍵播放兩次)


查看完整回答
反對(duì) 回復(fù) 2024-01-18
  • 2 回答
  • 0 關(guān)注
  • 256 瀏覽
慕課專欄
更多

添加回答

舉報(bào)

0/150
提交
取消
微信客服

購(gòu)課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動(dòng)學(xué)習(xí)伙伴

公眾號(hào)

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號(hào)