javascript 我有2警报去了,当应该只有一个,有人能告诉我为什么?

5us2dqdw  于 2023-04-28  发布在  Java
关注(0)|答案(1)|浏览(105)

我有一个石头纸sciccors游戏,所以我把js代码下面

var r = 0;
var p = 0;
var s = 0;
var num = 0;

function random() {
  var num = Math.floor(Math.random() * 3);
  console.log(`${num}`);
}

function rockcho() {
  let r = 1;
  let p = 0;
  let s = 0;

};

function papercho() {
  let p = 1;
  let r = 0;
  let s = 0;
};

function scissorscho() {
  let s = 1;
  let p = 0;
  let r = 0;
}

function play() {
  if (r = 0, p = 0, s = 0) {
    nothing();
  };
  //this is for rock//
  if (r = 1) {
    if (num = 2) {
      win();
      let rock = 0;
    };
    if (num = 1) {
      lose();
    }
    if (num = 0) {
      draw();
    }
  }
}

function win() {
  alert("you win! :) click to play again");
};

function lose() {
  alert("you lost :( click to play again");
};

function draw() {
  alert("it was a draw! click to play again");
};

function nothing() {
  alert("please click a button to play.")
};

胜利警报和失败警报同时响起。谁来帮帮忙。
我试着重写代码,它没有工作,我不知道如何修复警报错误,它会关闭每当我按下一个按钮时,它不应该。

tpgth1q7

tpgth1q71#

我修正了你的错误代码看看我的。_.

function play() {
if (r == 0 && p == 0 && s == 0) {
  nothing();
}

//this is for rock//
if (r == 1) {
  if (num == 2) {
    win();
    let rock = 0;
  }

  if (num == 1) {
    lose();
  }
  if (num == 0) {
    draw();
  }
 }
}

相关问题