Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/html/89.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript JS-石头、纸、剪刀、按钮而不是提示_Javascript_Html - Fatal编程技术网

Javascript JS-石头、纸、剪刀、按钮而不是提示

Javascript JS-石头、纸、剪刀、按钮而不是提示,javascript,html,Javascript,Html,我有一个基本的JS“石头,纸,剪刀”游戏的代码。它与promt一起工作,但我希望能够用按钮做出选择。我想使用“getElementById”和“addEventListener(“单击”)”。谁能给我指一下正确的方向吗 HTML: 岩石 纸 剪刀 JavaScript: var userChoice = prompt("Do you choose rock, paper or scissors?"); var computerChoice = Math.random(); if (co

我有一个基本的JS“石头,纸,剪刀”游戏的代码。它与promt一起工作,但我希望能够用按钮做出选择。我想使用“getElementById”和“addEventListener(“单击”)”。谁能给我指一下正确的方向吗

HTML:

岩石
纸 剪刀

JavaScript:

var userChoice = prompt("Do you choose rock, paper or scissors?");



var computerChoice = Math.random();
 if (computerChoice < 0.34) {
  computerChoice = "rock";

} else if(computerChoice <= 0.67) {
  computerChoice = "paper";

} else {
computerChoice = "scissors";
 } console.log("Computer: " + computerChoice);


var compare = function (choice1, choice2) {
 if (choice1 === choice2) {
  return "The result is a tie!";
  } 

  else if (choice1 === "rock") {
    if (choice2 === "scissors") {
      return "rock wins";
  }
   else {
      return "paper wins";


  } if (choice1 === "paper") {
   if (choice2 === "rock") {
        return "paper wins";
    }
     else {
        return "scissors wins";


    } if (choice1 === "scissors") {
        if (choice2 === "rock") {
            return "rock wins";

        } else if (choice2 === "paper") {
            return "scissors wins";
        }
        }
        }
      }
  } 
   compare (userChoice, computerChoice); 
var userChoice=prompt(“您选择石头、布还是剪刀?”);
var computerChoice=Math.random();
如果(计算机选择<0.34){
computerChoice=“rock”;

}else if(computerChoice在以下几个块中使用
onclick
事件处理程序():

document.getElementById('rock').onclick = function(e){
  userChoice = 'rock'
}

在以下几个块中使用
onclick
事件处理程序()

document.getElementById('rock').onclick = function(e){
  userChoice = 'rock'
}
下面是一个示例(单击“运行代码段”):

document.getElementById('rock').onclick=user;
document.getElementById('paper').onclick=user;
document.getElementById('scissors').onclick=user;
函数用户(){
var userChoice=this.id;
log(“用户:”+userChoice)
var computerChoice=Math.random();
如果(计算机选择<0.34){
computerChoice=“rock”;
}else if(computerChoice以下是一个示例(单击“运行代码片段”):

document.getElementById('rock').onclick=user;
document.getElementById('paper').onclick=user;
document.getElementById('scissors').onclick=user;
函数用户(){
var userChoice=this.id;
log(“用户:”+userChoice)
var computerChoice=Math.random();
如果(计算机选择<0.34){
computerChoice=“rock”;

}else if(computerChoice)我明白了,谢谢你指出了这一点,我可以问一下,你会如何在HTML上而不是控制台上表示输出吗?我明白了,谢谢你指出了这一点,我可以问一下,你会如何在HTML上而不是控制台上表示输出吗?