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

时间:2015-07-16 15:35:43

标签: javascript

我正在使用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;
    }
  }
}

1 个答案:

答案 0 :(得分: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;
}