想循环但变量未初始化

时间:2013-09-14 23:15:56

标签: java loops do-while

我的问题从第一个do-while循环开始。我想让它问“请输入0到90之间的角度:”如果用户确实输入了0到90之间的数字,他们必须输入枪粉量。他们的输入将通过物理计算,如果他们的结果不是“它是一个打击!”然后我希望他们回去再输入角度和火药。但问题出在

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

  

错误说明:变量distance2可能尚未初始化。


    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: ");
         }
      }
   }
}

2 个答案:

答案 0 :(得分:4)

在您的代码中 - distance2在其中一个条件块(在else循环内的while块中)内初始化时,可能会出现while或{{ 1}}条件将无法满足,在这种情况下else未初始化,局部变量无法在未初始化的情况下使用,因此只需将distance2初始化为任何有意义的或要求的默认值为:

distance2或您的代码逻辑可以使用的某些特定值

答案 1 :(得分:2)

distance2变量在try / catch块中初始化。可能会抛出异常,并在catch块初始化之前将控制流置于distance2块中。在catch块之后,distance2用于while循环结束时的计算,所以是:它可能不会在此行初始化:

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

如果您正在阅读NumberFormatException中的gunpowderangle值,则会引发input。还有一个if条件(angle上的边界检查)可能会使distance2未设置。