如何在java中处理符号未找到编译错误?

时间:2014-06-28 18:39:00

标签: java exception

是否发现错误ship1。 我只在符合条件时才声明ship1。 其他明智的我已经放置了重新运行该功能的其他条件。 这是编译问题,所以我之前被告知...尝试捕获将无法正常工作。

    public static int plantNavy(BattleBoard myBattle, int counter) {
    System.out.println("Im innnnn");
    if (counter == 0) {
        System.out.println("\nPlacing Large Ship");
    }
    else if (counter == 1) {
        System.out.println("Placing Medium Ship");
    }
    else if (counter == 2) {
        System.out.println("Placing Medium Ship");
    }
    else if (counter == 3) {
        System.out.println("Placing Small Ship");
    }
    else if (counter == 4) {
        System.out.println("Placing Small Ship");
    }

    System.out.println("Enter 0 to place ship horizontally");
    System.out.println("Enter 1 to place ship vertically");

    String align = shipAlignment.nextLine();
    if (align.length() > 1) {
        System.out.println("Inappropriate value entered. Please enter again");
        plantNavy(myBattle,counter);
    }

    if (align.charAt(0) - 48 == 0 || align.charAt(0) - 48 == 1) {
        if (align.charAt(0) - 48 == 0) {
            if (counter == 0) {
                BattleShip ship1 = new LargeShip(false);
            }
            else if (counter == 1) {
                BattleShip ship1 = new MediumShip(false);
            }
            else if (counter == 2) {
                BattleShip ship1 = new MediumShip(false);
            }
            else if (counter == 3) {
                BattleShip ship1 = new SmallShip(false);
            }
            else if (counter == 4) {
                BattleShip ship1 = new SmallShip(false);
            }
        }
        if (align.charAt(0) - 48 == 1) {
            if (counter == 0) {
                BattleShip ship1 = new LargeShip(true);
            }
            else if (counter == 1) {
                BattleShip ship1 = new MediumShip(true);
            }
            else if (counter == 2) {
                BattleShip ship1 = new MediumShip(true);
            }
            else if (counter == 3) {
                BattleShip ship1 = new SmallShip(true);
            }
            else if (counter == 4) {
                BattleShip ship1 = new SmallShip(true);
            }
        }
    }
    else {
        System.out.println("Inappropriate value entered");
        counter=plantNavy(myBattle,counter);
    }

        System.out.println("Enter Ship Placing position");
        String shipPos = shipPlace.next();
        if (shipPos.length() > 3 || shipPos.length() < 2) {
            System.out.println("Inappropriate target. Please enter again");
            counter = plantNavy(myBattle,counter);
        }
        else if ((int) (shipPos.charAt(1))-48 < 1 || (int)  shipPos.charAt(1)-48 > 10) {
            System.out.println("Inappropriate target. Please enter again");
            counter = plantNavy(myBattle,counter);
        }

        else if ((int) (shipPos.charAt(0)) < 65 || (int) shipPos.charAt(0)> 74) {
            System.out.println("Inappropriate target. Please enter again");
            counter = plantNavy(myBattle,counter);
        }

        int x_pos;
        int y_pos;

        if (shipPos.length() == 3) {
            shipPos = shipPos.charAt(0) + "10";
        }
        if (shipPos.length() == 2) {
            x_pos = (int) (shipPos.charAt(1))-49;
        }
        else {
            x_pos = 9;
        }
        y_pos = (int) (shipPos.charAt(0))-65;

        System.out.println(x_pos);
        System.out.println(y_pos);

        boolean plantCor = myBattle.addShip(ship1,x_pos,y_pos);

        if (plantCor == true) {
            System.out.println(myBattle.printActualBoard());
            counter++;
            return counter;
        }

        if (plantCor == false) {
            System.out.println("Incorrect Placement. Place Again in empty area.");
            counter = plantNavy(myBattle,counter);
        }
    }

1 个答案:

答案 0 :(得分:0)

在方法的顶部声明一个BattleShip ship1 = null变量并使用它:

public static int plantNavy(BattleBoard myBattle, int counter) {
   BattleShip ship1 = null;

不要在if块中重新声明变量,只需分配给它。即,

if (counter == 0) { 
   ship1 = new LargeShip(false); 
} 

不是

if (counter == 0) { 
   BattleShip ship1 = new LargeShip(false);  // note the difference!
} 

稍后您可以检查它是否为null,以查看是否已创建ship1。

if (ship1 == null) {
   // then no ship1 has been created yet.
}

修改
您在评论中说明:

  

@HovercraftFullOfEels我尝试了这种技术,但它给出了一个错误的返回语句错误。仅在成功放置船舶时才返回。

然后给它必要的退货声明。您的代码在if语句中嵌套了一堆。如果if语句都不成立,那么方法有可能在没有返回任何内容的情况下结束,并且这是不允许的。

一种解决方案是在方法的底部添加一个默认的return语句,但是如果这是我的代码,我会丢掉整个方法并重新编写它,将它重构为几个更简单的更简单的方法,并获得用户这种方法的互动。


编辑2
我看到的另一个问题是你以一种危险且不必要的方式使用递归:

  if (shipPos.length() > 3 || shipPos.length() < 2) {
     System.out.println("Inappropriate target. Please enter again");
     counter = plantNavy(myBattle, counter);
  }

请注意,如果输入了错误的输入,则您在同一方法中再次调用该方法,但没有意识到即使该方法将再次调用,在递归调用返回后,原始方法仍将运行 错误输入。我的建议,避免在这里递归,再次重构和简化你的代码将帮助你做到这一点。

相反,在单独的方法中获取用户输入,在获取输入时验证输入(简单的执行循环就足够了),然后一旦获得了放置船只所需的所有输入,请调用您的方法将没有的船舶置于其中的任何用户输入。


编辑3
接下来,您将想要摆脱那些正在使用的神奇数字,使您的代码成为理解和调试的负担。