if语句中的初始化变量,稍后在代码中使用,但表示它未定义

时间:2015-08-28 03:46:49

标签: java variables if-statement initialization

  import java.util.Scanner;
  import java.lang.Math;

  public class pizzasMazboudi
  {
     public static void main(String[] args)
     {
    double shapeOfPizza;
    double toppings;
    double pizzaCrust;
    double baseCost;
    double areaOfPizza;
    double numberOfToppings;
    final double COST_OF_ONE_TOPPING = 0.025;
    final double COST_OF_DOUGH = 0.019;
    final double COST_OF_SAUCE = 0.036;
    double diameterOfPizza;
    double lengthOfPizza;
    double widthOfPizza;
    double volumeOfDough;
    final double THIN_AND_CRISPY = .1;
    final double PAN = .5;
    final double CLASSIC_HAND_TOSSED = .25;
    final double TEXAS_TOAST = .9;
    double sizeOfCrust;
    double cheesyCrust;
    final double COST_OF_MATERIALS = .02;
    double numberOfPizzas;
    double costOfDelivery;
    double tax;
    double pizzaDelivery;
    final double PI = 3.14159;
    double typeOfCrust;
  double costOfCheesyCrust;
    double costOfPizzaDelivery;

  Scanner keyboard = new Scanner(System.in);
  System.out.println("Hello customer! Welcome to Guiseppi's Just Pizza!");
  System.out.println("Where we make the pizza just for you!");
  System.out.println("\n What kind of pizza do you want on" + 
                     " this beautiful day?");
  do
     {
     System.out.print("Press 1 for a square pizza,");  
     System.out.print(" or press 2 for a circle pizza: ");
     shapeOfPizza = keyboard.nextDouble();

        if(shapeOfPizza != 1 && shapeOfPizza != 2)
           {
           System.out.println("That is an incorrect input, please enter 1 or 2");
           }
      }  
  while(shapeOfPizza != 1 && shapeOfPizza != 2);

  if(shapeOfPizza == 1)
     {
     System.out.println("And how big would you like your pizza?");

        do
           {
           System.out.print("Please enter the length of your pizza (under 60 inches: ");
           lengthOfPizza = keyboard.nextDouble();

              if(lengthOfPizza > 60 && lengthOfPizza < 0)
                 {
                 System.out.println("That is an incorrect input, please enter a number between 0 and 60");
                 }
           }
        while(lengthOfPizza > 60 && lengthOfPizza < 0);

        do
           {   
           System.out.print("Please enter the width of your pizza: ");
           widthOfPizza = keyboard.nextDouble();

              if(widthOfPizza >60 && widthOfPizza <0)
                 {
                 System.out.println("That is an incorrect input, please enter a number between 0 and 60.");
                 }
           }
        while(widthOfPizza > 60 && widthOfPizza < 0);

        areaOfPizza = lengthOfPizza * widthOfPizza;
        System.out.println("Your pizza is " + areaOfPizza + "inches large");
        sizeOfCrust = 2 * (lengthOfPizza + widthOfPizza);
     }   

  else if(shapeOfPizza == 2)
     {
     System.out.println("And how big would you like your pizza?");
     do
        {
        System.out.print("Please enter the diameter of your pizza: ");
        diameterOfPizza = keyboard.nextDouble();
           if(diameterOfPizza > 60 && diameterOfPizza <= 0)
              {
              System.out.println("That is an incorrect input, please enter a number that is between 0 and 60.");
              }
        }
     while(diameterOfPizza > 60 && diameterOfPizza <= 0);

        sizeOfCrust = 2*PI*(diameterOfPizza/2);
        areaOfPizza = PI*((diameterOfPizza/2)*(diameterOfPizza/2));
        System.out.println("The area of your pizza is" + areaOfPizza + " inches large");
     }

  System.out.println("What type of crust would you like?");
  do
     {
     System.out.println("Enter 1 for Thin and Crispy, 2 for Pan,"); 
     System.out.println("3 for Classic Hand-Tossed, and 4 for Texas Toast");
     pizzaCrust = keyboard.nextDouble();
        if(pizzaCrust != 2 && pizzaCrust != 3 && pizzaCrust != 4)
           {
           System.out.println("That is an incorrect input, please enter 1, 2, 3, or 4");
           }
     }
  while(pizzaCrust != 2 && pizzaCrust != 3 && pizzaCrust != 4);

  if(pizzaCrust == 1);
     {
     typeOfCrust = THIN_AND_CRISPY;
     }
  if(pizzaCrust == 2);
     {
     typeOfCrust = PAN;
     }
  if(pizzaCrust == 3);
     {
     typeOfCrust = CLASSIC_HAND_TOSSED;
     }
  if(pizzaCrust == 4);
     {
     typeOfCrust = TEXAS_TOAST;
     }

  if(pizzaCrust == 2 && pizzaCrust == 3 && pizzaCrust == 4)
     {
     System.out.println("Would you like to try our new cheesy crust today?");
     do
        {
        System.out.print("Enter 1 for yes or 2 for no");
        cheesyCrust = keyboard.nextDouble();
           if(cheesyCrust != 1 && cheesyCrust != 2)
              {
              System.out.println("That is an incorrect input, please enter 1 or 2");
              }
        }
     while(cheesyCrust != 1 && cheesyCrust != 2);
     }
  System.out.println("Would you like any toppings today?");
  do
     {
     System.out.println("Enter 1 for toppings, enter 2 if you do not want toppings");
     toppings = keyboard.nextDouble(); 
        if(toppings != 1 && toppings !=2)
           {
           System.out.println("That is an incorrect input, please enter 1 or 2");
           }
     }
  while(toppings != 1 && toppings !=2);

  if(toppings == 1)
     {
        do
           {
           System.out.println("How many toppings do you want? (10 toppings limit)");
           numberOfToppings = keyboard.nextDouble();
              if(numberOfToppings <=0 && numberOfToppings > 10)
                 {
                 System.out.println("That is an incorrect input, please enter a number 1 through ten");
                 }
           }
        while(numberOfToppings <=0 && numberOfToppings > 10);
     }

  do
     {
     System.out.println("How many identical pizza's would you like?");
     numberOfPizzas = keyboard.nextDouble();
        if(numberOfPizzas <= 0)
           {
           System.out.println("That is an incorrect input, please enter a number above 0");
           }
     }
  while(numberOfPizzas <= 0);

  System.out.println("Will you come pick up your order, or will you like delivery?");
  do
     {
     System.out.println("Enter 1 for delivery, enter 2 for pick-up");
     pizzaDelivery = keyboard.nextInt();
        if(pizzaDelivery != 1 && pizzaDelivery != 2)
           {
           System.out.println("That is an incorrect input, please enter a 1 or 2");
           }
     }
  while(pizzaDelivery != 1 && pizzaDelivery != 2);

  costOfCheesyCrust = sizeOfCrust * COST_OF_MATERIALS;
  volumeOfDough = typeOfCrust * areaOfPizza;
  baseCost = areaOfPizza * (COST_OF_SAUCE + numberOfToppings * COST_OF_ONE_TOPPING) + COST_OF_DOUGH * volumeOfDough;

  if(cheesyCrust == 1)
     {
     costOfCheesyCrust = sizeOfCrust * COST_OF_MATERIALS;
     baseCost = baseCost + costOfCheesyCrust;
     }
  if(pizzaDelivery == 1)
     {
     if(baseCost < 10)
        {
        costOfPizzaDelivery = 3.00;
        }
     else if(baseCost < 20 || baseCost >= 10)
        {
        costOfPizzaDelivery = 2.00;
        }
     else if(baseCost < 30 || baseCost >= 20)
        {
        costOfPizzaDelivery = 1.00;
        }
     else if(baseCost >= 30)
        {
        costOfPizzaDelivery = 0;
        }
     }
 }

}

因此,当我尝试编译时,变量numberOfTopping,areaOfPizza和sizeOfCrust都是错误的原因,因为&#34;它们没有被初始化&#34;,但我已经将它们全部定义为双精度和将numberOfTopping设置为keyboard.nextDouble并给出其他2个公式,任何帮助将不胜感激。

2 个答案:

答案 0 :(得分:3)

编译器是正确的。

numberOfToppings在您的标头中定义,但是如果toppings == 1

进入循环,分配了一个值

稍后当您计算baseCost时,您使用numberOfToppings的值,此时代码中的用户可能选择不进行任何配料,而numberOfToppings已从未被分配过初始值。

为避免这种情况,您应该在标题中将变量初始化为所需的默认值 0

double numberOfToppings = 0;

这同样适用于您提及的其他人,原因相同。根据用户输入,这些变量可能永远不会被初始化,但您仍然会尝试在最终计算中使用它们;你应该把它们全部初始化。

作为旁注,您应该学会在IDE中使用自动格式化程序,以获得更好的代码可读性。

答案 1 :(得分:0)

在声明这些变量时,应该使用一些默认值初始化这些变量。

您的代码很乱,但这些变量可能永远不会根据用户输入设置,所以基本上您不能使用未初始化的变量。

作为最佳实践,在使用之前不要声明局部变量,这可能会导致增加其范围。当您需要使用变量时,应立即声明并初始化它。