Search code examples
javabooleantext-based

More while issues for my text based game


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


Solution

  • 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");