2014-06-28 80 views
-8

是给出错误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); 
     } 
    } 
+7

这是编译时错误。异常处理是一个运行时概念。 –

+0

什么是'例外'。它应该是'Exception'。和括号.. –

+2

为什么你需要处理的情况下,当一个变量没有找到?为什么不直接定义呢? –

回答

0

在你的方法顶部声明战舰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! 
} 

以后你可以检查它是否是空,看是否SHIP1已创建。

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

编辑
幽州的评论:

@HovercraftFullOfEels我尝试这种技术,但它给缺少return语句的错误。只有在船舶成功放置后才能返回。

然后给它必要的返回语句。你的代码有一堆返回嵌套在if语句中。如果if语句都不是真的,那么方法有可能结束而不返回任何内容,这是不允许的。

一种解决方案是在方法底部添加一个默认返回语句,但如果这是我的代码,我会查看整个方法并重新编写它,将其重构为几个更小的更简单的方法,用户交互出这个方法。


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

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

需要注意的是如果输入错误的输入,你打电话该方法再次在相同的方法内,但没有意识到,即使该方法将再次调用,在递归调用返回后,原始方法仍将运行完成错误输入。我的建议,避免在这里递归,再次重构和简化你的代码将帮助你做到这一点。

取而代之的是,获取用户输入的一个单独的方法,验证输入,因为它正在被获取(一个简单的do-while循环就足够了),然后一旦获得了所有必要的放置船的输入,调用你的方法放置一艘而没有其中有任何用户输入。


编辑3
下一步,你想摆脱那些神奇的数字,你正在使用的,使你的代码熊理解和调试。

+0

谢谢。你的编辑2解决了我的问题。我在每个递归语句 –

+0

@ user3005132之后添加了返回计数器:您的代码可能会编译,但该递归是有风险的下注,并且可能会咬你。 –

+0

我被困在另一个问题。你能帮我通过电子邮件吗?我现在得到太多的票,我不能问问题。 @气垫船 –