是给出错误ship1未找到。 我只在符合条件时声明ship1。 其他明智的我已经放置其他条件,重新运行的功能。 这是编译问题,所以我早先被告知...尝试捕捉将无法正常工作。如何处理符号未找到java中的编译错误?
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);
}
}
这是编译时错误。异常处理是一个运行时概念。 –
什么是'例外'。它应该是'Exception'。和括号.. –
为什么你需要处理的情况下,当一个变量没有找到?为什么不直接定义呢? –