是給出錯誤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'。和括號.. –
爲什麼你需要處理的情況下,當一個變量沒有找到?爲什麼不直接定義呢? –