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

How to loop if/else statements (JavaScript)?

本文关键字:else 语句 JavaScript if 循环 何循环      更新时间:2023-09-26

我正在创造一款基于文本的探索类型游戏。我想知道如何循环if/else语句以及重置变量。我有它,所以你扮演的角色在他的卧室开始,你必须输入命令,让你的角色跟随,比如环顾四周。如果你还能帮助我java识别相同的单词在所有类型的形式(在任何地方的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循环和变量来完成此操作。我也会去掉所有的if语句用switch语句

代替

这里有一些信息

  • Switch语句
  • While循环

我还用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;
    }
}

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

也许这对你有帮助

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

如果你喜欢这个没有烦人的脚本框,给我一个呼号,相信我能让它工作。