为什么每次都会打印这个else语句?

时间:2016-08-29 05:18:36

标签: javascript

我正在制作一个简单的文字游戏。出于某种原因,当我输入正确的文本时,将始终输出else文本以及正确的输出文本。只有输入了错误的文本时才会显示else文本。我究竟做错了什么?

//take commands
if (input == "text 1" && currentroom == "area_1")
    $("<p>reply 1.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
else if (input == "text 1" && "currentroom" != "area_1")
    $("<p>reply 2.</p>").hide().insertBefore("#placeholder").fadeIn(1000);


//got to room 
if (input == "go to area 2" && "currentroom" == "area_1") {
    currentroom = "area_outside";
    $("<p>You are now  in area 2.</p>");
} else if (input == "go to area 2" && "currentroom" != "area_1")
    $("<p>You cant go that way.</p>").hide().insertBefore("#placeholder").fadeIn(1000);

if (input == "go to area 1" && "currentroom" == "area_2") {
    currentroom = "area_outside";
    $("<p>You are back in area 1.</p>")
} else { //invaild commands/dont understand.
    $("<p>"+input+" is not vaild. type help for vaild commands.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}

4 个答案:

答案 0 :(得分:4)

最后else 与此if相关联:

if (input == "go to area 1" && "currentroom" == "area_2")

因此,如果input == "go to area 1"为false或"currentroom" == "area_2"为false(它始终为false),则会运行else代码。

你可能意味着

if (input == "go to area 1" && currentroom == "area_2")
// No quotes ------------------^----------^

在那里和其他几个地方,你可能或者可能不想在你使用else if的几个地方使用if

所以也许:

if (input == "text 1" && currentroom == "area_1") {
    $("<p>reply 1.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
else if (input == "text 1" && currentroom != "area_1") {
    $("<p>reply 2.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
else if (input == "go to area 2" && currentroom == "area_1") {
    currentroom = "area_outside";
    $("<p>You are now  in area 2.</p>")
}
else if (input == "go to area 2" && currentroom != "area_1") {
    $("<p>You cant go that way.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
else if (input == "go to area 1" && currentroom == "area_2") {
    currentroom = "area_outside";
    $("<p>You are back in area 1.</p>")
}
else {
    //invaild commands/dont understand.
    $("<p>" + input + " is not vaild. type help for vaild commands.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}

......虽然你可以稍微重构一下。

答案 1 :(得分:3)

您需要else if而不是if

//take commands
if (input == "text 1" && currentroom == "area_1") {
    $("<p>reply 1.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
else if (input == "text 1" && "currentroom" != "area_1") {
    $("<p>reply 2.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}

//got to room 
else if (input == "go to area 2" && "currentroom" == "area_1") {
    currentroom = "area_outside";
    $("<p>You are now  in area 2.</p>")
}
else if (input == "go to area 2" && "currentroom" != "area_1") {
    $("<p>You cant go that way.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}
else if (input == "go to area 1" && "currentroom" == "area_2") {
    currentroom = "area_outside";
    $("<p>You are back in area 1.</p>")
}

//invaild commands/dont understand.
else {
    $("<p>"+input+" is not vaild. type help for vaild commands.</p>").hide().insertBefore("#placeholder").fadeIn(1000);
}

答案 2 :(得分:0)

"currentroom" == "area_2"

这总是错误的。如果currentroom是一个变量,则从中删除引号。

答案 3 :(得分:0)

只是展示我的意思

  

您应该将任何DOM操作与游戏引擎分开。   另一种情况,你会得到一些不受支持的东西。

简单的步骤是提取回复方法:

var game = {
  reply: function(msg) {
    $("<p>"+msg+"</p>")
      .hide()
      .insertBefore("#placeholder")
      .fadeIn(1000);
  }
};

现在我们可以简单地编写逻辑:

switch(input) {
  case 'text 1':
    game.reply(currentroom == 'area_1' ? "reply 1." : "reply 2.");
  break;
  case 'go to area 2':
    switch(currentroom) {
      case 'area_1':
        currentroom = 'area_outside';
        game.reply("You are now in area 2.");
      break;
      default:
        game.reply("You cant go that way.");
    }
  break;
  default:
    game.reply('Wrong input');
}

然后我们可以填补移动等等:

game.moveTo = function(room) {
  // game normally should to know available rooms list
  // if (!this.rooms[room]) return this.reply('Unknown room ' + room);
  if (room === this.currentRoom) return this.reply('You are already here!');
  this.currentRoom = room;
  this.reply('You now in '+room);
}