'else if'语句总是返回true

时间:2016-02-01 23:31:35

标签: java

每当我运行此程序行69的else if (room == 6)语句时,无论房间的值如何,它都会返回true

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

2 个答案:

答案 0 :(得分:2)

我看到你正在改变其他一些房间的价值 - 如果是的话。那可能会搞乱后续的if-if块吗?尝试使用switch ... case语句。

示例:

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: - )

答案 1 :(得分:0)

您可以使用单独的方法来处理每个房间中发生的事情,然后使用这些方法的数组或ArrayList(例如,称为“roomMethod”)。这些方法中的每一个都返回用户选择的下一个房间。然后主要代码是:

  while (room != 0 ) {
    room = roomMethod[room]();
  }