Home > other >  How to run if once, then run else forever
How to run if once, then run else forever

Time:03-11

I'm making an escape room through the console.

I'm trying to run an if statement, and I want to have the if statement code run once, and then run the else code forever afterward. My program is looped to go back to the same menu after it's finished with a choice the user chooses.

Here is my current code:

if (checkBedDraw = false) {

    clear();
    typePrint("You found a \u001b[34mpicklock\u001b[0m! you can only use this on a specific lock, because of its shape. This item has been added to your \u001b[31minventory\u001b[0m.", 50);
    invArray[0] = "picklock";
    checkBedDraw = true;
      
  } else if (checkBedDraw = true) {
  typePrint("You have already checked the drawers, and aqcuired a picklock.", 50);
}

typePrint() and clear() are methods I made.

CodePudding user response:

= and ==

Be careful, in if statements you have to use == as = is used to assign a value to a variable.

Also, you are dealing with Boolean so you can just do something like this:

if (checkBedDraw) {
    typePrint("You have already checked the drawers, and aqcuired a picklock.", 50);
} else {
    clear();
    typePrint("You found a \u001b[34mpicklock\u001b[0m! you can only use this on a specific lock, because of its shape. This item has been added to your \u001b[31minventory\u001b[0m.", 50);
    invArray[0] = "picklock";
    checkBedDraw = true;
}

CodePudding user response:

You can try to use "if" for the first statement and then "while" to create an infinite loop. Here the code!:

if(checkBedDraw == false){
              clear();
              typePrint("You found a \u001b[34mpicklock\u001b[0m! you can only use this on a specific lock, because of its shape. This item has been added to your \u001b[31minventory\u001b[0m.", 50);
              invArray[0] = "picklock";
              checkBedDraw = true;
            }
            while (checkBedDraw == true) {
              print('"You have already checked the drawers, and aqcuired a picklock."');
            }
  •  Tags:  
  • java
  • Related