当前位置:  开发笔记 > 编程语言 > 正文

否则没有错误

如何解决《否则没有错误》经验,为你挑选了1个好方法。

所以这是我的代码.

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

如果没有错误我一直没有得到别的东西,我不知道如何解决它.粗体代码是发生错误的地方.我一直在研究这个问题几个小时,我仍然可以开始工作.拜托,拜托,请帮帮我.



1> FrustratedWi..:

我认为问题是你有一个else if跟随else.尝试将其移动到此序列else最后一个.

此外,您有两个else相同的声明if:

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

这将导致其他问题,实际上甚至没有意义.

推荐阅读
135369一生真爱_890
这个屌丝很懒,什么也没留下!
DevBox开发工具箱 | 专业的在线开发工具网站    京公网安备 11010802040832号  |  京ICP备19059560号-6
Copyright © 1998 - 2020 DevBox.CN. All Rights Reserved devBox.cn 开发工具箱 版权所有