【问题标题】:'else if' statement always returns true'else if' 语句总是返回 true
【发布时间】:2016-05-10 14:33:46
【问题描述】:

每当我运行这个程序第 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.");
    }
}

【问题讨论】:

  • 第 69 行是哪一行?
  • 它的 if/else if 地狱....
  • 哇,吃饼干会死。这太苛刻了。
  • 你错过了 5 号房间的choice = keyboard.next();
  • @Joormatt 作为旁注,我建议使用switch/case 而不是这么多if/else if 语句(请参阅this)。

标签: java


【解决方案1】:

我看到你正在改变一些 else-if 中的房间值。这可能会干扰后续的 else-if 块吗?尝试使用 switch...case 语句。

示例:

而(房间!= 0){
    开关(房间){

        情况1: {
            System.out.println("你发现自己在一个房间里。有一个\"蓝色\"门和一个\"红色\"门。\n你选择哪个?");
            选择 = 键盘.next();

            if (choice.equalsIgnoreCase("red")) {
                房间 = 2;
            } else if (choice.equalsIgnoreCase("blue")) {
                房间 = 3;
            }
            休息;
        }

        案例2:{
            //房间==2 代码
            休息;
        }

        案例 3:{
            //等等...
            休息; //不要忘记在每个case的右括号前加上“break”
        }
    }
}

第 :-)

【讨论】:

  • 刚做了这个,它帮助简化了它并解决了 intellij 的问题,尽管它的 case n: 不是 case:n
【解决方案2】:

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

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

【讨论】:

  • 好吧,我还没有进入数组,但是当我这样做时,我会回顾一下,谢谢
  • @Joormatt 在 java 中不起作用。 FredK 最有可能用于编写 javascript 代码。
猜你喜欢
  • 2016-01-04
  • 2015-12-16
  • 1970-01-01
  • 1970-01-01
  • 2012-09-08
  • 1970-01-01
  • 1970-01-01
  • 2016-09-03
  • 2021-08-31
相关资源
最近更新 更多