Warning: file_get_contents(/data/phpspider/zhask/data//catemap/4/jsp/3.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 否则没有if错误_Java - Fatal编程技术网

Java 否则没有if错误

Java 否则没有if错误,java,Java,这是我的代码 public static void play(Player player) { Scanner localScanner = new Scanner(System.in); while(localScanner.hasNextLine()){ String input = localScanner.nextLine(); if(input.equals("quit")) { System.out.println("Game

这是我的代码

public static void play(Player player) {
    Scanner localScanner = new Scanner(System.in);
    while(localScanner.hasNextLine()){
      String input = localScanner.nextLine();
      if(input.equals("quit")) {
        System.out.println("Game over, Good bye.");
        return; 
      }
      if(input.contains("north")){
        buildWorld(player, player.moveTo().connectNorth());
      } else if(input.contains("south")){
        buildWorld(player, player.moveTo().connectSouth());
      } else if(input.contains("east")){
        buildWorld(player, player.moveTo().connectEast());
      } else if(input.contains("west")){
        buildWorld(player, player.moveTo().connectWest());
      } else {
        String contents;
        if (!contents.equals(""))
          System.out.println("There is:\n" + contents);
        else{
          System.out.println("This room is empty.");
       } else if(input.startsWith("pickup")){  // <-- This is the 'orphan' else
        contents = input.substring(8);
        if(player.moveTo().moveTo(contents)){
          Object localObject = player.connectWest(contents);
          if(localObject !=null)
            System.out.println("You have picked up " +localObject);
          else
            System.out.println("You have too much damage, Game over.");}
        else {
          System.out.println("There is no " +contents);
      }} else if (input.startsWith("drop")){
        contents=input.substring(6);
        if(player.moveTo(contents))
          System.out.println("you dropped " +contents);
        else
          System.out.println("You don't have "+ contents); 
      } else if(input.contains("status")){
        System.out.println(player);}
      else{
        System.out.println("What?");}
    }
  }
  } 
公共静态无效播放(播放器){
Scanner localScanner=新的扫描仪(System.in);
while(localScanner.hasNextLine()){
字符串输入=localScanner.nextLine();
if(input.equals(“quit”)){
System.out.println(“游戏结束,再见”);
回来
}
if(input.contains(“北”)){
buildWorld(player,player.moveTo().connectNorth());
}else if(input.contains(“南”)){
buildWorld(player,player.moveTo().connectSouth());
}else if(input.contains(“东”)){
buildWorld(player,player.moveTo().connectEast());
}else if(input.contains(“west”)){
buildWorld(player,player.moveTo().connectWest());
}否则{
字符串内容;
如果(!contents.equals(“”)
System.out.println(“有:\n”+内容);
否则{
System.out.println(“这个房间是空的。”);

}否则如果(input.startsWith(“pickup”){/这部分就是问题所在:

    if (!contents.equals(""))
      System.out.println("There is:\n" + contents);
    else{
      System.out.println("This room is empty.");}
    else if(input.startsWith("pickup")) {
    // ...
    }

else
之后不能有
else if
我想问题是在
else
之后有一个
else if
。尝试将
else
移动到该序列的最后一个

此外,在相同的
if
后面还有两个
else
语句:

else {
    String contents;
    ...
}
...
else{
    System.out.println("What?");}

这将导致额外的问题,甚至毫无意义。使用标准代码格式将有所帮助。你的缩进和括号放置是如此随机,我们无法在不阅读所有内容的情况下确定…你就是不能做一个
else
-
else if
标准else基本上是if-else链中的全部。哟else是最后通牒,如果交换它们,就不能再使用else。大多数IDE甚至不允许您尝试编译语法错误类型的代码…我建议使用Eclipse作为IDE