Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

'else if' statement always returns true

Tags:

java

Whenever I run this program line 69's else if (room == 6) statement, it always returns true, regardless of the value of room.

import java.util.Scanner;

public class AdventureTwo {
    public static void main(String[] args) {
        Scanner keyboard = new Scanner(System.in);
        String choice = "";
        int room = 1;

        while (room != 0) {
            if (room == 1) {
                System.out.println("You find yourself in a room. There's a \"blue\" door and a \"red\" door.\nWhich do you choose?");
                choice = keyboard.next();

                if (choice.equalsIgnoreCase("red")) {
                    room = 2;
                } else if (choice.equalsIgnoreCase("blue")) {
                    room = 3;
                }
            } else if (room == 2) {
                System.out.println("Going through the red door reveals the \"family\" room.\nYou can also go \"back\". What is your choice?");
                choice = keyboard.next();
                if (choice.equalsIgnoreCase("family")) {
                    room = 4;
                } else if (choice.equalsIgnoreCase("back")) {
                    room = 1;
                }
            } else if (room == 3) {
                System.out.println("You find two new rooms, the \"kitchen\" and the \"bathroom\".\nYou can also go \"back\", What is your choice?");
                choice = keyboard.next();
                if (choice.equalsIgnoreCase("kitchen")) {
                    room = 5;
                } else if (choice.equalsIgnoreCase("bathroom")) {
                    room = 6;
                } else if (choice.equalsIgnoreCase("back")) {
                    room = 1;
                }
            } else if (room == 4) {
                System.out.println("In the family room you see a tv \"remote\" do you pick it up and turn on the tv, or go \"back\"?");
                choice = keyboard.next();
                if (choice.equalsIgnoreCase("remote")) {
                    System.out.println("You pick up the remote and turn on the tv. You are immediately engrossed in the rv program.");
                    System.out.println("In fact you are so interested you stay there watching tv until you die of dehydration");
                    room = 0;
                } else if (choice.equalsIgnoreCase("back")) {
                    room = 2;
                }
            } else if (room == 5) {
                System.out.println("In the kitchen you find some \"crackers\", you are pretty hungry and they don't look that old.\nYou can also go \"back\".");
                if (choice.equalsIgnoreCase("crackers")) {
                    System.out.println("After eating one cracker, you feel the need to have another one, and another , and another, this goes on until you\nhave eaten so much your stomach bursts and you die.");
                    room = 0;
                } else if (choice.equalsIgnoreCase("back")) {
                    room = 3;
                }
            } else if (room == 6) { // line 69
                System.out.println("In the bathroom there is an open \"window\", you can also go \"back\". What is your choice?");
                choice = keyboard.next();
                if (choice.equalsIgnoreCase("window")) {
                    System.out.println("You climb out the window and drop to the ground. You are free!");
                    room = 0;
                } else if (choice.equalsIgnoreCase("back")) {
                    room = 3;
                }
            }
        }
        System.out.print("Game Over.");
    }
}
like image 643
Joormatt Avatar asked Jul 13 '26 21:07

Joormatt


1 Answers

I see you are changing the value of room in some of the else-if's. Could that be messing with subsequent else-if blocks? Try using the switch...case statement.

example:

while (room != 0) {
    switch(room) {

        case 1: {
            System.out.println("You find yourself in a room. There's a \"blue\" door and a \"red\" door.\nWhich do you choose?");
            choice = keyboard.next();

            if (choice.equalsIgnoreCase("red")) {
                room = 2;
            } else if (choice.equalsIgnoreCase("blue")) {
                room = 3;
            }
            break;
        }

        case 2: {
            //room==2 code
            break;
        }

        case 3: {
            //etc...
            break; //don't forget to put the "break" before each case's close bracket
        }
    }
}

hth :-)

like image 88
n8bar Avatar answered Jul 16 '26 14:07

n8bar



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!