【问题标题】:How to loop if/else statements (JavaScript)?如何循环 if/else 语句(JavaScript)?
【发布时间】:2014-09-15 10:29:31
【问题描述】:

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

    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.')
}

【问题讨论】:

  • while(true) 包裹整个代码。或者改变真实,这样你就可以真正结束游戏了。 (while(stillPlaying)),并且在您的条件之一内,您可以将 stillPlaying 设置为 false。至于大写问题,只需将 begin.toLowerCase() 改为 begin.toLowerCase(),并确保您与之比较的字符串始终为小写。
  • 我还会在您检查它们的值之前将您的字符串值转换为小写。这样,无论是看、看还是看都没有关系。如果这些值是从某种类型的命令行中提取的,我会在您检索它们后立即转换它们。
  • 您可以简单地使用包含所有选项的数组和 indexOf 来检查数组中是否存在选项,而不是使用 if-else。
  • 使用递归函数调用。

标签: javascript loops if-statement reset


【解决方案1】:

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

【讨论】:

  • 虽然是一个很好的建议,但您的答案可以充实以包含一个示例。
  • uhh 在交换机外中断还是在while 外中断?您的答案应该详细说明,甚至可能提供样本,因为它可能是 1 或 2 班轮。
  • 正如 Jasonscript 所说,我不想做他的工作,只是告诉他如何去做......
  • 如果您不打算扩展,这实际上应该是一条评论。另外,当您说break 时,它是模棱两可的,所以这就是为什么我问您是要突破时间还是突破开关。
【解决方案2】:

您可以使用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;
    }
}

【讨论】:

    【解决方案3】:

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

    【讨论】:

      【解决方案4】:

      这可能对你有帮助

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

      【讨论】:

        【解决方案5】:

        给它一个小结构,这样你就有了一些事情要做。过去,* 的很多人给了我足够多的工作,但对他们的感激之情还是不够的,我有点回馈了:)

        一个好方法是将它全部放在一个函数中并让它自己调用。赋予游戏一些维度,并让您返回之前的位置。

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

        您还可以通过以下方式在提示中预填充值:

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

        如果您希望这个没有烦人的脚本框,请给我打个招呼,相信我可以让它工作。

        【讨论】: