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

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

將開(kāi)始按鈕從html鏈接到j(luò)avascript函數(shù)以執(zhí)行開(kāi)始游戲的提示,提示在頁(yè)面開(kāi)始時(shí)開(kāi)始

將開(kāi)始按鈕從html鏈接到j(luò)avascript函數(shù)以執(zhí)行開(kāi)始游戲的提示,提示在頁(yè)面開(kāi)始時(shí)開(kāi)始

素胚勾勒不出你 2022-12-29 15:08:44
我創(chuàng)建了一個(gè)非常基本的剪刀石頭布射擊游戲,您可以在其中使用 JavaScript 函數(shù)與計(jì)算機(jī)對(duì)戰(zhàn)。我有一個(gè)非?;镜?HTML 框架,其中有一個(gè)按鈕。我試圖在按下時(shí)啟動(dòng)提示(游戲)。提示按我想要的方式工作,但一旦我打開(kāi)窗口它就會(huì)啟動(dòng)腳本。我試圖僅在按下 html 上的按鈕時(shí)啟動(dòng)腳本。當(dāng)我按下按鈕時(shí),控制臺(tái)出現(xiàn)錯(cuò)誤getPlayerChoice 未在 HTMLButtonElement.onclick 中定義這是我的代碼:let playerWinCount = 0;let computerWinCount = 0;let roundCount = 0;function playRound() {function computerPlay() {    let arr = ["Rock", "Paper", "Scissors"];    let compChoice = arr[Math.floor(Math.random() * arr.length)];        return compChoice;}//function getPlayerChoice() {    var str = prompt("What is your selection for this round?");    if (!str) {        return;}    let newStr = str.toLowerCase();    let capStr = newStr[0].toUpperCase() + newStr.slice(1);        if (capStr !== "Rock" && capStr!== "Paper" && capStr !== "Scissors") {        return;} else {        return capStr;    }}//    let playerSelection = getPlayerChoice();    let computerSelection = computerPlay();                    if (playerSelection === computerSelection) {        alert("Try again... You both chose " + playerSelection + "!");        return;} else {        if (playerSelection === "Rock") {        if (computerSelection === "Scissors") {        alert("You win! Rock beats Scissors!");        playerWinCount++;        roundCount++;        console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);        return;} else if (computerSelection === "Paper") {        alert("You lose! Paper beats Rock!");        computerWinCount++;        roundCount++;        console.log("Computer wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);        return;    }    } else  if (playerSelection === "Paper") {        if (computerSelection === "Rock") {        alert("You win! Paper beats Rock!");        playerWinCount++;        roundCount++;        console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);        return;    } 我注釋掉了我的提示功能,因?yàn)槊慨?dāng)程序運(yùn)行時(shí),它都會(huì)啟動(dòng)無(wú)法關(guān)閉的提示。如果沒(méi)有輸入,我需要一種關(guān)閉提示窗口的方法。
查看完整描述

1 回答

?
慕標(biāo)琳琳

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

您可能希望在game單擊 Start Game 按鈕時(shí)運(yùn)行該函數(shù),而不是在頁(yè)面加載時(shí)運(yùn)行它。


<input id="Start Game" type="button" value="Start Game" onclick="game();" />

現(xiàn)場(chǎng)示例:


let playerWinCount = 0;

let computerWinCount = 0;

let roundCount = 0;


function playRound() {

  function computerPlay() {

    let arr = ["Rock", "Paper", "Scissors"];

    let compChoice = arr[Math.floor(Math.random() * arr.length)];

    return compChoice;

  }


  function getPlayerChoice() {

    var str = prompt("What is your selection for this round?");

    if (!str) {

      return;

    }

    let newStr = str.toLowerCase();

    let capStr = newStr[0].toUpperCase() + newStr.slice(1);

    if (capStr !== "Rock" && capStr !== "Paper" && capStr !== "Scissors") {

      return;

    } else {

      return capStr;

    }

  }

  //

  let playerSelection = getPlayerChoice();

  let computerSelection = computerPlay();

  if (playerSelection === computerSelection) {

    alert("Try again... You both chose " + playerSelection + "!");

    return;

  } else {

    if (playerSelection === "Rock") {

      if (computerSelection === "Scissors") {

        alert("You win! Rock beats Scissors!");

        playerWinCount++;

        roundCount++;

        console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);

        return;


      } else if (computerSelection === "Paper") {

        alert("You lose! Paper beats Rock!");

        computerWinCount++;

        roundCount++;

        console.log("Computer wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);

        return;

      }


    } else if (playerSelection === "Paper") {

      if (computerSelection === "Rock") {

        alert("You win! Paper beats Rock!");

        playerWinCount++;

        roundCount++;

        console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);

        return;


      } else if (computerSelection === "Scissors") {

        alert("You lose! Scissors beats Paper!");

        computerWinCount++;

        roundCount++;

        console.log("Computer wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);

        return;

      }

    } else if (playerSelection === "Scissors") {

      if (computerSelection === "Rock") {

        alert("You lose! Rock beats Scissors!");

        computerWinCount++;

        roundCount++;

        console.log("Computer wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);

        return;

      } else if (computerSelection === "Paper") {

        alert("You win! Scissors beats Paper!");

        playerWinCount++;

        roundCount++;

        console.log("Player wins! Current score: Player-" + playerWinCount + ", Computer-" + computerWinCount);

        return;

      }

    }

  }

}

//


function game() {

  while (roundCount < 5) {

    playRound();

  }

  if (playerWinCount > computerWinCount) {

    alert("Player wins! The score was " + playerWinCount + " - " + computerWinCount);

  } else if (computerWinCount > playerWinCount) {

    alert("Computer wins! The score was " + computerWinCount + " - " + playerWinCount);

  } else {

    alert("Something crazy happened and I have no idea who won!");

  }

}

<input id="Start Game" type="button" value="Start Game" onclick="game()" />


查看完整回答
反對(duì) 回復(fù) 2022-12-29
  • 1 回答
  • 0 關(guān)注
  • 104 瀏覽
慕課專(zhuān)欄
更多

添加回答

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