当前位置: 代码迷 >> JavaScript >> 如何使用JavaScript在另一个if语句中创建if语句?
  详细解决方案

如何使用JavaScript在另一个if语句中创建if语句?

热度:55   发布时间:2023-06-12 13:37:59.0

我正在用HTML CSS和Javascript制作扑克游戏。 现在,我正在实际处理卡片的程序中。 我需要在另一个if语句中包含一个if语句。 当它运行时,它会生成一个随机数,然后我有一个if语句列表,该语句首先查看是否已选择卡,然后在第一个if语句中包含另一个if语句,以查看是否已处理卡。 我不知道为什么它不起作用。 有人可以帮忙吗? 这是代码:

var yourchipsvar = 500;
var alchipsvar = 500;
var potvar = 0;
var whotodealto = 0;

yourchipsvar = Number(yourchipsvar);
alchips = Number(alchipsvar);
potvar = Number(potvar);

document.getElementById("yourchips").innerHTML = Number(yourchipsvar);
document.getElementById("alchips").innerHTML = Number(alchipsvar);
document.getElementById("pot").innerHTML = Number(potvar);


function bet() {

  yourchipsvar = yourchipsvar - 5;
  potvar = potvar + 5;
  yourchipsvar = Number(yourchipsvar);
  potvar = Number(potvar);
  document.getElementById("pot").innerHTML = potvar;
  document.getElementById("yourchips").innerHTML = yourchipsvar;
}

function bet15() {

  yourchipsvar = yourchipsvar - 15;
  potvar = potvar + 15;
  yourchipsvar = Number(yourchipsvar);
  potvar = Number(potvar);
  document.getElementById("pot").innerHTML = potvar;
  document.getElementById("yourchips").innerHTML = yourchipsvar;
}

function bet25() {

  yourchipsvar = yourchipsvar - 25;
  potvar = potvar + 25;
  yourchipsvar = Number(yourchipsvar);
  potvar = Number(potvar);
  document.getElementById("pot").innerHTML = potvar;
  document.getElementById("yourchips").innerHTML = yourchipsvar;
}

function bet50() {

  yourchipsvar = yourchipsvar - 50;
  potvar = potvar + 50;
  yourchipsvar = Number(yourchipsvar);
  potvar = Number(potvar);
  document.getElementById("pot").innerHTML = potvar;
  document.getElementById("yourchips").innerHTML = yourchipsvar;
}
var aceofspades = 0;
var twoofspades = 0;
var threeofspades = 0;
var fourofspades = 0;
var fiveofspades = 0;
var cardinhand = 0;

function deal() {
  if (whotodealto == 0) {
    dealuser();
  } else {
    alert("dealtotheAI");
  }
}

function dealuser() {
  var cardinhand = Math.floor(Math.random() * 6);

  if (cardinhand == 0) {
    if (aceofspades == 0) {
      alert("You got an ace of spades");
      var aceofspades = 1;
    }
  }

  if (cardinhand == 1) {
    if (twoofspades == 0) {
      alert("You got a two of spades");
      var twoofspades = 1;
    }
  }

  if (cardinhand == 2) {
    if (threeofspades == 0) {
      alert("You got a three of spades");
      var threeofspades = 1;
    }
  }

  if (cardinhand == 3) {
    if (fourofspades == 0) {
      alert("You got a four of spades");
      var fourofspades = 1;
    }
  }

  if (cardinhand == 4) {
    if (fiveofspades == 0) {
      alert("You got a five of spades");
      var fiveofspades = 1;
    }
  }
}

您正在为每个aceofspadestwoofspades等创建两个不同的局部变量。这是因为var表示要在您所在的范围内创建局部变量。因此,此处定义的变量是:

var aceofspades = 0;
var twoofspades = 0;
var threeofspades = 0;
var fourofspades = 0;
var fiveofspades = 0;
var cardinhand = 0;

function deal() {
    if(whotodealto==0){dealuser();}  else{alert("dealtotheAI");}}

function dealuser() {
    var cardinhand = Math.floor(Math.random()*6);

    ...
}

与您在此处创建的内容不同:

 function dealuser() {
    ...
    if(aceofspades==0){
        alert("You got an ace of spades"); 
        var aceofspades = 1;
    }

在这种情况下, var aceofspades意味着您要在dealuser()函数中创建一个局部变量。 只需设置已经创建的变量的值,就可以避免创建新变量:

if(aceofspades==0) {
    alert("You got an ace of spades"); 
    aceofspades = 1;
}
  相关解决方案