0
while(!A){ 
           System.out.println ("You enter a room and look around, in it, you see three     doors, a red door labeled A, a blue door labeled B, and a green door labeled C.  Which door do you choose to go through? Enter, A, B, or C");
           String correctdoor = scanner.next();

           A = "A".equalsIgnoreCase(correctdoor);
           System.out.println("You have chosen wrong! You have fallen into a pit! Lucky for you though, the pit is easy to climb out of and you return to the room....\n\n\n");           
        }

    System.out.println("You progress through the door and find 5 light switches, you must turn them on in order to progress further. Enter the correct combination (using digits 1-5) here please.  HINT - the 2nd and 4th numbers add up to the first number and the last number is NOT 5");   
    int lightcode = scanner.nextInt();
    while (!(lightcode == 31425)){System.out.println ("That combination is incorrect");}
    System.out.println ("The door unlocks and you go down a set of stairs");

Hey, back again for more help.

The while statement does work as intended when the user inputs b or c or any other value not A, but when they DO put A it does take them out of the while loop, but it still prints the 'you have chosen wrong' string. I'm not sure why, but I AM sure you guys can tell me why.

Furthermore, the second loop works perfectly if I input the right number, the only issue is that when I do NOT, it DOES tell me 'the combo is incorrect' but it doesnt just print it once, it keeps printing it and wont stop, its neverending. What did I do wrong?

Maybe I should use an if statement? Eh...no...that wont loop...ugh.

ps I know I said the last number isnt 5, but ill fix it in post

METEORITES
  • 43
  • 1
  • 6

2 Answers2

0

I looked at your code, and came up with this, it worked for me and fixed the problem you described:

while(!A)
{ 
       System.out.println ("You enter a room and look around, in it, you see three     doors, a red door labeled A, a blue door labeled B, and a green door labeled C.  Which door do you choose to go through? Enter, A, B, or C");
       String correctdoor = scanner.next();

       A = "A".equalsIgnoreCase(correctdoor);

       if (!A) // Added this here, displays the message only if they chose the incorrect door
       {
           System.out.println("You have chosen wrong! You have fallen into a pit! Lucky for you though, the pit is easy to climb out of and you return to the room....\n\n\n");
       }
}

Now, for the second part, here is what I did, which also fixed the problem you described:

int lightcode = 0; //Initialize lightcode to something incorrect here
while (!(lightcode == 31425))
{
 System.out.println("You progress through the door and find 5 light switches, you must turn them on in order to progress further. Enter the correct combination (using digits 1-5) here please.  HINT - the 2nd and 4th numbers add up to the first number and the last number is NOT 5");   
    lightcode = scanner.nextInt(); //Get lightcode from the player

    if (!(lightcode == 31425)) //And finally, only if the code is INCORRECT, display the incorrect message
    {
     System.out.println ("That combination is incorrect");
    }
}

System.out.println ("The door unlocks and you go down a set of stairs");
Shadow
  • 3,926
  • 5
  • 20
  • 41
  • So I DID have to use an if statement! At least I was on the right track, thank you so much! these work wonderfully and now I understand where I made my mistake. – METEORITES Sep 28 '14 at 06:20
0

Ok, here's how I'd do this:

while(true){ 
  System.out.println ("You enter a room and look around, in it, you see three     doors, a red door labeled A, a blue door labeled B, and a green door labeled C.  Which door do you choose to go through? Enter, A, B, or C");
  String correctdoor = scanner.next();
  if("A".equalsIgnoreCase(correctdoor)) {
    break;
  }
  System.out.println("You have chosen wrong! You have fallen into a pit! Lucky for you though, the pit is easy to climb out of and you return to the room....\n\n\n");
}

I understand that intui!(lightcode == 31425)tively, setting the condition in while loop looks like the right way to go but the problem is, that condition is kind of useless as you have the flow control happening inside of the while loop. So, I just made this more clear: Keep looping forever and if correctdoor matches "A" break the loop

for your second problem, you can use the very same approach, because the logic is the same: loop forever until you get what you expect.

System.out.println("You progress through the door and find 5 light switches, you must turn them on in order to progress further. Enter the correct combination (using digits 1-5) here please.  HINT - the 2nd and 4th numbers add up to the first number and the last number is NOT 5");   
int lightcode = 0;
while (true){
  lightcode = scanner.nextInt();
  if(lightcode == 31425) {
    break;
  }
  System.out.println ("That combination is incorrect");
}
  System.out.println ("The door unlocks and you go down a set of stairs");

If you the first statement for the keys to be repeated everytime, just put it in the beginning of the second while. Good luck with your game making! And just in case, here's a gist with the working code I wrote https://gist.github.com/dallarosa/14617052520c571ad2ad

DallaRosa
  • 5,737
  • 2
  • 35
  • 53
  • 1
    Hey...I like how this looks! Thank you so much for helping me! I'm learning a lot, I know enough basics to get moving on this game but I'm running into some issues perfecting my loops. This way looks quite simple to remember and I'll probably be using it for the rest of the project. – METEORITES Sep 28 '14 at 07:47