Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/318.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java ';如果';语句始终返回true_Java - Fatal编程技术网

Java ';如果';语句始终返回true

Java ';如果';语句始终返回true,java,Java,每当我运行程序第69行的else if(room==6)语句时,无论room的值是多少,它总是返回true import java.util.Scanner; public class AdventureTwo { public static void main(String[] args) { Scanner keyboard = new Scanner(System.in); String choice = ""; int room

每当我运行程序第69行的
else if(room==6)
语句时,无论room的值是多少,它总是返回
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.");
    }
}

我看到您正在更改某些其他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 } } } while(房间!=0){ 开关(室){ 案例1:{ System.out.println(“您发现自己在一个房间里,有一个“蓝色”门和一个“红色”门。\n您选择哪一个?”); 选择=键盘。下一步(); if(选择等信号情况(“红色”)){ 房间=2间; }else if(选择.相等信号情况(“蓝色”)){ 房间=3间; } 打破 } 案例2:{ //房间==2码 打破 } 案例3:{ //等等。。。 break;//不要忘记在每个案例的右括号前加上“break” } } }
hth:-)

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

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

第69行是哪一行?它的
if/else if
hell…哇,饼干消费导致的死亡。太残酷了。你错过了
choice=keyboard.next()在房间5。@Joormatt作为旁注,我建议使用
switch/case
而不是这么多
if/else if
语句(请参阅)。我还没有谈到数组,但当我谈到数组时,我会回顾一下,thanks@Joormatt这在java中是行不通的。FredK很可能习惯于用javascript编写代码。就这样做了,它帮助简化了代码,并解决了intellij的问题,尽管它的
case n:
不是
case:n