0

My problem starts at the first do-while loop. I want it to ask "Please enter an angle between 0 an 90: " and if the user does input a number between 0 and 90, they have to input the amount of gun powder. Their inputs will go through the physics calculations and if their outcome is not "It's a hit!" then I want them to go back and input the angle and gunpowder again. But the thing is at the

while (distance - distance2 != 0 || distance - distance2 != 1 || distance2 - distance != 1); System.out.println("It's a hit!");

The error says: variable distance2 might not have been initialized.


    import java.util.*;
    import java.text.*;
    public class BombsAway {
    private static double ZERO_THOUSAND = 1000;
    private static double KG_TO_VELOCITY = 50;
    private static double GRAVITY = -9.81;
    public static void main(String[] args) {

      Scanner input = new Scanner(System.in);
      DecimalFormat threeDec = new DecimalFormat("#.##");
      Random gen = new Random();
      BombsAway distanceAn = new BombsAway();
      boolean invalidInput = true;
      double distance, distance2;

      System.out.println("Please enter a positive integer seed value: ");
      while(invalidInput) {
         try {
            int seedValue = Integer.valueOf(input.nextLine());
            if (seedValue <= 0) {
               System.out.println("Please enter a positive integer seed value:"
                + " ");
            } 
            else {
               distance = gen.nextDouble() * ZERO_THOUSAND;
               System.out.println("That target is " + threeDec.format(distance) 
                + "m away.");  
               do {
               System.out.println("Please enter an angle between 0 and 90 " +
                "degrees: ");
               while (invalidInput) {
                  try {
                     double angle = Double.valueOf(input.nextLine());
                     if (angle < 0 || angle > 90) {
                        System.out.println("Please enter an angle between " +
                         "0 and 90 degrees: ");
                     } 
                     else {
                        System.out.println("Please enter the amount of " + 
                         "gunpowder in kilograms: ");
                        try {
                           double gunpowder = 
                            Double.valueOf(input.nextLine());
                           //physics part
                           double initialV = gunpowder * KG_TO_VELOCITY;
                           double vX = initialV * Math.cos(angle);
                           double vY = initialV * Math.sin(angle);
                           double airBorn = (-vY - vY) / GRAVITY;
                           distance2 = (vX * airBorn);

                           if (distance > distance2) {
                              double finalDist = distance - distance2;
                              System.out.println("It's " +
                               threeDec.format(finalDist) + "m short.");
                           }
                           else if (distance < distance2) {
                              double finalDist = distance2 - distance;
                              System.out.println("It's " + 
                               threeDec.format(finalDist) + "m long.");
                           }
                        }
                        catch(NumberFormatException e) {
                           System.out.println("Please enter the amount of " +
                            "gunpowder in kilograms: ");
                        }
                     }
                  }
                  catch(NumberFormatException e) {
                      System.out.println("Please enter an angle between " +
                       "0 and 90 degrees: ");
                  }
               }
            }while (distance - distance2 != 0 || distance - distance2 != 1
              || distance2 - distance != 1);
                System.out.println("It's a hit!");
            }
         }
         catch(NumberFormatException e) {
            System.out.println("Please enter a positive integer seed value: ");
         }
      }
   }
}
4

2 回答 2

4

在您的代码中 -distance2在其中一个条件块中初始化(在循环内的else块中while)可能会出现您的whileorelse条件不会被满足的情况,在这种情况下distance2未初始化并且局部变量不能在没有初始化的情况下使用,因此只需初始化具有distance2任何有意义或必需的默认值作为:

double distance2 = 0.0;或具有您的代码逻辑适用的某些特定值

于 2013-09-14T23:20:20.953 回答
2

distance2变量在 try/catch 块中初始化。可能会引发异常,并在初始化catch之前将控制流降落在块中。distance2在 catch 块之后, distance2 用于循环结束时的计算while,所以是的:它可能没有在这一行初始化:

while (distance - distance2 == 0 || distance - distance2 == 1
      || distance2 - distance == 1)

如果NumberFormatException在您从 中读取gunpowderorangle值时抛出了 a input。还有一个 if 条件(边界检查 on angle)可能会使 distance2 未设置。

于 2013-09-14T23:20:21.953 回答