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

如何处理java中的符号未找到编译错误?,java,exception,Java,Exception,Is给出了找不到ship1的错误。 我只有在符合条件的情况下才申报装船。 另一方面,我放置了else条件,重新运行函数。 这是一个编译问题,所以正如前面告诉我的。。。试抓不起作用 public static int plantNavy(BattleBoard myBattle, int counter) { System.out.println("Im innnnn"); if (counter == 0) { System.out.println("\n

Is给出了找不到ship1的错误。 我只有在符合条件的情况下才申报装船。 另一方面,我放置了else条件,重新运行函数。 这是一个编译问题,所以正如前面告诉我的。。。试抓不起作用

    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);
        }
    }
public static int plantNavy(作战板myBattle,int计数器){
System.out.println(“Im innnn”);
如果(计数器==0){
System.out.println(“\n放置大型船舶”);
}
else if(计数器==1){
System.out.println(“放置介质船”);
}
否则如果(计数器==2){
System.out.println(“放置介质船”);
}
否则如果(计数器==3){
System.out.println(“放置小型船舶”);
}
否则如果(计数器==4){
System.out.println(“放置小型船舶”);
}
System.out.println(“输入0以水平放置船舶”);
System.out.println(“输入1以垂直放置船舶”);
String align=shipAlignment.nextLine();
如果(align.length()>1){
System.out.println(“输入的值不正确,请重新输入”);
海军(myBattle,counter);
}
if(align.charAt(0)-48==0 | | align.charAt(0)-48==1){
如果(对齐字符(0)-48==0){
如果(计数器==0){
战列舰ship1=新的慷慨(假);
}
else if(计数器==1){
战列舰ship1=新中型舰(假);
}
否则如果(计数器==2){
战列舰ship1=新中型舰(假);
}
否则如果(计数器==3){
战列舰ship1=新的小型战舰(假);
}
否则如果(计数器==4){
战列舰ship1=新的小型战舰(假);
}
}
如果(对齐字符(0)-48==1){
如果(计数器==0){
战列舰ship1=新的慷慨(正确);
}
else if(计数器==1){
战列舰ship1=新中型舰(正确);
}
否则如果(计数器==2){
战列舰ship1=新中型舰(正确);
}
否则如果(计数器==3){
战列舰ship1=新的小型战舰(正确);
}
否则如果(计数器==4){
战列舰ship1=新的小型战舰(正确);
}
}
}
否则{
System.out.println(“输入的值不正确”);
计数器=工厂海军(myBattle,计数器);
}
System.out.println(“输入船舶放置位置”);
字符串shipPos=shipPlace.next();
如果(shipPos.length()>3 | | shipPos.length()<2){
System.out.println(“目标不正确,请重新输入”);
计数器=工厂海军(myBattle,计数器);
}
如果((int)(shipPos.charAt(1))-48<1 | |(int)shipPos.charAt(1)-48>10){
System.out.println(“目标不正确,请重新输入”);
计数器=工厂海军(myBattle,计数器);
}
如果((int)(shipPos.charAt(0))<65 | |(int)shipPos.charAt(0)>74){
System.out.println(“目标不正确,请重新输入”);
计数器=工厂海军(myBattle,计数器);
}
int x_pos;
国际邮政局;
如果(发货位置长度()=3){
shipPos=shipPos.charAt(0)+“10”;
}
如果(shipPos.length()=2){
x_pos=(int)(shipPos.charAt(1))-49;
}
否则{
x_pos=9;
}
y_pos=(int)(shipPos.charAt(0))-65;
系统输出打印LN(x_位置);
系统输出打印项次(y_位置);
布尔plantCor=myBattle.addShip(ship1,x_位置,y_位置);
如果(plantCor==true){
System.out.println(myBattle.printActualBoard());
计数器++;
返回计数器;
}
如果(plantCor==false){
System.out.println(“位置不正确。再次放置在空白区域。”);
计数器=工厂海军(myBattle,计数器);
}
}

在方法顶部声明战列舰ship1=null变量并使用它:

public static int plantNavy(BattleBoard myBattle, int counter) {
   BattleShip ship1 = null;
不要在if块中重新声明变量,只需分配给它即可。i、 例如,做什么

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

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

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

编辑
您在评论中声明:

@HovercraftFullOfEels我尝试了这种技术,但它给出了一个缺少返回语句的错误。只有在船舶成功放置的情况下,才可返回

然后给出必要的返回语句。您的代码在if语句中嵌套了一系列返回。如果If语句都不为true,则方法有可能在不返回任何内容的情况下结束,这是不允许的

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


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

  if (shipPos.length() > 3 || shipPos.length() < 2) {
     System.out.println("Inappropriate target. Please enter again");
     counter = plantNavy(myBattle, counter);
  }
if(shipPos.length()>3 | | shipPos.length()<2){
System.out.println(“目标不正确,请重新输入”);
计数器=工厂海军(myBattle,计数器);
}
请注意,如果输入了错误的输入,您将在同一个方法中再次调用该方法,但没有意识到即使该方法将再次调用,在递归调用返回后,原始方法仍将在错误的输入下运行。我的建议是,在这里避免递归,再次重构和简化代码将帮助您做到这一点

相反,在一个单独的方法中获取用户输入,在获得输入时验证输入(一个简单的do-while循环就足够了),然后在放置一艘船所需的所有输入完成后
  if (shipPos.length() > 3 || shipPos.length() < 2) {
     System.out.println("Inappropriate target. Please enter again");
     counter = plantNavy(myBattle, counter);
  }