Java 跳过代码中If-Else部分的程序出现问题

Java 跳过代码中If-Else部分的程序出现问题,java,user-input,Java,User Input,我正在创建一个基于文本的游戏,到目前为止,我已经使用了if和else语句。但是,else if不起作用。我浏览了好几个网站和页面,甚至与一位同事交谈过。我已经做了我能做的,比如用?.equalsCall替换==然后改为Call.equals?。我将else-if作为嵌套if,但也失败了 public static void FirstRoom(){ System.out.println("You are in a room filled with\ndarkness and no mem

我正在创建一个基于文本的游戏,到目前为止,我已经使用了if和else语句。但是,else if不起作用。我浏览了好几个网站和页面,甚至与一位同事交谈过。我已经做了我能做的,比如用?.equalsCall替换==然后改为Call.equals?。我将else-if作为嵌套if,但也失败了

public static void FirstRoom(){
    System.out.println("You are in a room filled with\ndarkness and no memory of your\npast. There are several objects\nscattered on the ground and a\nlantern in your hand. You find\na lighter in your pocket.");
    System.out.println("\n ----- \n");
    Scanner console = new Scanner(System.in);
    String Call = console.next();
    if (Call.equals("Inspect")) {
        System.out.println("\nYou can only see darkness");
        System.out.println("\n ----- \n");
    } else if(Call.equals("Light Lantern")){
        System.out.println("\nYou pull the lighter from your pocket and,\nturning the knob, raise the\nlantern's wick and light it.");
    System.out.println("\n ----- \n");
    }else{
        System.out.println("\nWhat do you mean?");
        System.out.println("\n ----- \n");
    FirstRoom();
   }
   String Call2 = console.next();
   if(Call.equals("Inspect")) {
       System.out.println("You see a large sword to your\nright and a bag to the left.\nThere is a door in front\n of you and a door behind.");
       System.out.println("\n ----- \n");
   }
}
当我输入Light Lantern时,我希望它会说你从口袋里掏出打火机,转动旋钮,升起灯笼的灯芯并点燃它。相反,它跳转到第二条语句,该语句也应该等待您输入第二条语句,并说您看到右侧有一把大刀,左侧有一个袋子。你前面有一扇门,后面有一扇门

检查

你只能看到 黑暗

灯 你看到一把大刀对着你的眼睛 右边和左边各一个袋子。 前面有一扇门 你和后面的一扇门


您需要使用console.nextLine而不是console.next

String Call /*bad name*/ = console.next();
只会取第一个字光

String call = console.nextLine(); //using normal JAVA naming conventions
将把所有内容都带到行尾字符,这意味着您将能够输入多个单词,如Light Lantern


有关next和nextLine之间差异的更多信息,请查看此

您需要使用console.nextLine而不是console.next

String Call /*bad name*/ = console.next();
只会取第一个字光

String call = console.nextLine(); //using normal JAVA naming conventions
将把所有内容都带到行尾字符,这意味着您将能够输入多个单词,如Light Lantern


有关next和nextLine之间差异的更多信息,您可以查看此

您正在停止接收字符串Call2=console.next的输入;在检查调用是否等于Inspect之前。你应该查一下电话号码

这将是我重新设计该方法的建议:

public static void FirstRoom() {
    System.out.println("You are in a room filled with\ndarkness and no memory of your\npast. There are several objects\nscattered on the ground and a\nlantern in your hand. You find\na lighter in your pocket.");
    System.out.println("\n ----- \n");
    Scanner console = new Scanner(System.in);
    String Call;
    boolean exitRoom = false;
    boolean lantern = false;
    while(!exitRoom) {
        Call = console.nextLine();
        if(Call.equals("Inspect") && !lantern) {
            System.out.println("\nYou can only see darkness");
            System.out.println("\n ----- \n");
        } else if(Call.equals("Light Lantern")) {
            System.out.println("\nYou pull the lighter from your pocket and,\nturning the knob, raise the\nlantern's wick and light it.");
            System.out.println("\n ----- \n");
            lantern = true;
        } else if(Call.equals("Inspect") && lantern) {
            System.out.println("You see a large sword to your\nright and a bag to the left.\nThere is a door in front\n of you and a door behind.");
            System.out.println("\n ----- \n");
        } else if(Call.equals("Leave")) {
            System.out.println("You leave the room");
            System.out.println("\n ----- \n");
            exitRoom = true;
        } else {
            System.out.println("\nWhat do you mean?");
            System.out.println("\n ----- \n");
            FirstRoom();
        }
    }
}

当请求离开房间的命令时,while循环结束

您正在停止接收字符串Call2=console.next的输入;在检查调用是否等于Inspect之前。你应该查一下电话号码

这将是我重新设计该方法的建议:

public static void FirstRoom() {
    System.out.println("You are in a room filled with\ndarkness and no memory of your\npast. There are several objects\nscattered on the ground and a\nlantern in your hand. You find\na lighter in your pocket.");
    System.out.println("\n ----- \n");
    Scanner console = new Scanner(System.in);
    String Call;
    boolean exitRoom = false;
    boolean lantern = false;
    while(!exitRoom) {
        Call = console.nextLine();
        if(Call.equals("Inspect") && !lantern) {
            System.out.println("\nYou can only see darkness");
            System.out.println("\n ----- \n");
        } else if(Call.equals("Light Lantern")) {
            System.out.println("\nYou pull the lighter from your pocket and,\nturning the knob, raise the\nlantern's wick and light it.");
            System.out.println("\n ----- \n");
            lantern = true;
        } else if(Call.equals("Inspect") && lantern) {
            System.out.println("You see a large sword to your\nright and a bag to the left.\nThere is a door in front\n of you and a door behind.");
            System.out.println("\n ----- \n");
        } else if(Call.equals("Leave")) {
            System.out.println("You leave the room");
            System.out.println("\n ----- \n");
            exitRoom = true;
        } else {
            System.out.println("\nWhat do you mean?");
            System.out.println("\n ----- \n");
            FirstRoom();
        }
    }
}

当请求离开房间的命令时,while循环结束

问题1:这不是JavaScriptAlway,如果您选择不跟随Java变量,即所有变量都以小写字母开头,那么您可能最终会使用一些自动导入IDE调用某个外部类的静态函数。问题1:这不是JavaScriptAlway,如果您选择不遵循Java变量,即所有变量都以小写字母开头,那么您可能最终会使用一些自动导入IDE调用某个外部类的静态函数。谢谢您的回答。然而,这是一个我用来学习if-else-if-else循环的项目。到目前为止,我还没有使用while循环。不过,我会研究这些问题。再一次,谢谢。这个while循环只是为了让您可以在不需要复制任何代码的情况下放置所有各种调用响应行为。尝试使用它,我想您会发现行为符合您的预期。谢谢您的回答。然而,这是一个我用来学习if-else-if-else循环的项目。到目前为止,我还没有使用while循环。不过,我会研究这些问题。再次感谢您。这个while循环只是为了让您可以在不需要复制任何代码的情况下放置所有不同的调用响应行为。尝试使用它,我认为您会发现行为符合您的期望。