繁体   English   中英

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

[英]How to loop if/else statements (JavaScript)?

好的,所以我正在创建一个基于文本的探索式游戏。 我想知道如何循环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.')
}

我会在一段时间内推荐一个开关(true),如果有什么事情发生,那就休息吧。

考虑更好地构建代码。 将数据放在适当的结构上,并使用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);
            }
        }
    }
}

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

这是一些信息

我还通过alert更改了一些confirm消息

这是结果

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;
    }
}

这可能对你有所帮助

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
    }
}

给它一个小结构,所以你有一些东西要离开。 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);
    }
}

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

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM