如何循环if / else语句(JavaScript)?

时间:2014-07-24 02:11:24

标签: javascript loops if-statement reset

好的,所以我正在创建一个基于文本的探索式游戏。我想知道如何循环if / else语句以及重置变量。我拥有它所以你在他的卧室里开始扮演的角色,你必须输入命令让你的角色跟随,如环顾四周。如果您也可以帮助我识别所有类型的表格中的相同单词(在任何地方都有CAPS)。

    if (begin === 'look around')
{
  confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
} 
else if (begin === 'look')
{
  confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
} 
else if (begin === 'examine room')
{
  confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
} 
else if (begin === 'examine the room')
{
  confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
} 
else if (begin === '?')
{
  confirm('If you have not already noticed, this game is completely Text-based. In order to progress through the game, you will need to type in commands that you think the character you are playing as should do.Example: look around.')
}

5 个答案:

答案 0 :(得分:3)

我会建议在一段时间内切换(true),如果有什么事情发生,只需要休息。

答案 1 :(得分:0)

考虑更好地构建代码。 将数据放在适当的结构上,并使用while语句作为主游戏循环。

参见示例:

var game = [
    {
        input: ['look', 'look around'],
        answer: 'You see a lot of cool stuff'
    },
    {
        input: ['?', 'help'],
        answer: 'Good luck'
    }
]

var entry = "";
while (entry !== "exit") {
    entry = prompt("What now?", "help").toLowerCase();
    for (var i = 0; i < game.length; i++) {
        for (var j = 0; j < game[i].input.length; j++) {
            if (game[i].input[j] === entry) {
                confirm(game[i].answer);
            }
        }
    }
}

答案 2 :(得分:0)

您可以使用while循环和变量来执行此操作。 我也会删除所有这些if语句并替换为switch语句

这里有一些信息

我还使用confirm

更改了部分alert条消息

这是结果

var exit = false;

while (!exit) {
    switch (begin)
    {
        case 'look around':
            alert('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed');
            break;

        case 'look':
          alert('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed');
          break;

        case 'examine room':
          alert('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed');
          break;

        case 'examine the room':
          alert('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed');
          break;

        case 'exit':
            if (confirm('Are you sure you want to exit this game?')){
                exit = true;
            }
            break;

        case '?':
        default:
          alert('If you have not already noticed, this game is completely Text-based. In order to progress through the game, you will need to type in commands that you think the character you are playing as should do.Example: look around.');
          break;
    }
}

答案 3 :(得分:0)

可能这可能会对你有所帮助

var t=true;
while(t)
{
    if (begin === 'look around')
    {
      confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
    } 
    else if (begin === 'look')
    {
      confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
    } 
    else if (begin === 'examine room')
    {
      confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
    } 
    else if (begin === 'examine the room')
    {
      confirm('To your right is the door.Left is the dresser with some pictures on it and a mirror on the wall. In front of you is the cabinets with a TV on top. Behind you is the bed')
    } 
    else if (begin === '?')
    {
      confirm('If you have not already noticed, this game is completely Text-based. In order to progress through the game, you will need to type in commands that you think the character you are playing as should do.Example: look around.')
      t=false; // if you want to come out of the loop
    }
}

答案 4 :(得分:0)

给它一个小结构,所以你有一些东西要离开。 stackoverflow的很多人在过去给了我足够多的工作,并且不能再感谢他们了,关于我回馈一点点的时间:)

一个好方法是将它全部放在一个函数中并让它自己调用。为游戏提供一些尺寸,让您返回以前的位置。

编辑时要小心,需要确保可以退出函数/循环。

您还可以通过执行以下操作在提示中包含预填充值:

prompt("Text", "Text in box");

让用户在知道您期望的内容时更轻松。

var place = "room";
var playing = true;

// Get user's first choice
var choice = prompt("You are in a room with some choices");

// Call the function once to start the game
gameLoop(playing, place, choice);

// your main game loop
function gameLoop (playing, place, choice) {

   // first "room"
   while(playing && place == "room") {

      // where the users "choice" gets evaluated
      switch(choice) {
        case "mirror": 
        choice = prompt("You have chosen to look at the mirror. Nothing, where to now?");
        break;

        case "leave":
        place = "hallway";
        choice = prompt("You have chosen to leave the room. In the doorway you can go left or right, which way?");
        break;

        case "dresser": 
        choice = prompt("You have chosen to look at the dresser");
        break;

        case "exit":
        playing = false;
        break

        default:
        choice = prompt("Yo what do you wanna do?");
       }
    } 

   // second "room"
   while (playing && place == "hallway") {
     switch(choice) {
        case "left": 
        choice = confirm("You went left down the hallway and fell through the floor and met an untimely death.");
        playing = false;
        break;

        case "back":
        place = "room";
        var choice = prompt("You went back and are in a room with some choices");
        break;

        case "right":
        confirm("A smiling Unicorn, you won!");
        break;

        default:
        playing = false;
      }
    }

    // loop the game with the place and choice if playing is true, else game is over
    if (playing) {
       gameLoop(playing, place, choice);
    }
}

如果你没有烦人的脚本框就喜欢这个,请给我一个大声喊叫,相信我能让它发挥作用。