无法初始化变量

时间:2016-02-27 00:13:14

标签: java

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Assignment3 
{
 public static void main(String[] args)
 {
   double width;
   double length;
   double height;
   double weight;
   double totalDimen;         // (width * length * height) + weight
   double shipping;
   double totalCost;
   double cost12 = 12;
   double cost14 = 14;
   double cost17 = 17;
   double cost21 = 21;
   double cost33 = 33;
   double cost105 = 105;
   double surcharge;
   int zipCode;               //gets the zip code
   char zipCode2;

   String input;

   //changing the decimal format
   DecimalFormat formatter = new DecimalFormat("#0.00");


   //getting weight
   input = JOptionPane.showInputDialog("Enter the total weight");
   weight = Double.parseDouble(input);

   //getting height
   input = JOptionPane.showInputDialog("Enter the total height");
   height = Double.parseDouble(input);

   //getting length
   input = JOptionPane.showInputDialog("Enter the total length");
   length = Double.parseDouble(input);

   //getting width
   input = JOptionPane.showInputDialog("Enter the total width");
   width = Double.parseDouble(input);

   //getting zipCode
   input = JOptionPane.showInputDialog("Enter Zip Code");
   zipCode = Integer.parseInt(input);
   zipCode = (char)zipCode;

    //getting zipCode - now gives zipCode a CHAR value, which I can use to compare.
   zipCode2 = ((String)JOptionPane.showInputDialog("Re-Type Zip Code")).charAt(0);
   JOptionPane.showMessageDialog(null, "Zip Code: " + zipCode2);

//===================================================================================   

   //Display weight, height, lenth, width |||| DEBUG
   JOptionPane.showMessageDialog(null, "Here is your input" + "\n" + weight + "\n" + width + "\n" + height + "\n" + length + "\n" + "Zip Code: " +zipCode);

   //getting total dimensions
   totalDimen = (length * width * height) + weight;
   JOptionPane.showMessageDialog(null, "Total Dimensions:\n" + totalDimen); //DEBUG
//====================================================================================   

   //if weight is <= 5 pounds, shipping = $12.00
   if ( weight <= 5 )
   {
      shipping = 12;
      JOptionPane.showMessageDialog(null, "Shipping is: " + cost12);
   }

   //if weight is > 5 pounds and <= 15 pounds, shipping = $14.00 
   if ( weight > 5 && weight <= 15 )
   {
      shipping = 14;
      JOptionPane.showMessageDialog(null, "Shipping is: " + cost14);
   }

   //if weight is > 15 pounds and <= 34 pounds, shipping = $17.00
   if ( weight > 15 && weight <= 34 )
   {
      shipping = 17;
      JOptionPane.showMessageDialog(null, "Shipping is: " + cost17);
   }

   //if weigth is > 34 pounds and <= 45 pounds, shipping = $21.00
   if ( weight > 34 && weight <= 45 )
   {
      shipping = 21;
      JOptionPane.showMessageDialog(null, "Shipping is: " + cost21);
   }

   //if weigth is > 45 pounds and <= 60 pounds, shipping = $33.00
   if ( weight > 45 && weight <= 60 )
   {
      shipping = 33;
      JOptionPane.showMessageDialog(null, "Shipping is: " + cost33);
   }

   //if weigth is > 60 pounds, shipping = $105.00
   if ( weight > 60 )
   {
      shipping = 105;
      JOptionPane.showMessageDialog(null, "Shipping is: " + cost105);
   }
//====================================================================================   

   //applying the surcharge using zip code
   if ( zipCode2 == 4 )
   {
      surcharge = (shipping * .05);
      JOptionPane.showMessageDialog(null, "Your surcharge cost at 5% is: $" + surcharge);
   }
   else if ( zipCode2 == 9 )
   {
      surcharge = (shipping * .09);
      JOptionPane.showMessageDialog(null, "Your surcharge cost at 9% is: $" + surcharge);
   }
   else
   {
      surcharge = (shipping * .14);
      JOptionPane.showMessageDialog(null, "Your surcharge cost at 14% is: $" + surcharge);
   }
//====================================================================================   

   //if the zip code is even, + 2% additional surcharge fee 


 }
}

这是输出错误:

Assignment3.java:122: error: variable shipping might not have been initialized
      surcharge = (shipping * .05);
                   ^
Assignment3.java:127: error: variable shipping might not have been initialized
      surcharge = (shipping * .09);
                   ^
Assignment3.java:132: error: variable shipping might not have been initialized
      surcharge = (shipping * .14);

我该怎么做才能解决这个问题?我希望它转到if语句,获取运费变量,然后转到下一个if语句,它根据邮政编码决定附加费率。

键入此内容时我做了一些tweeking: 我不知道它是否更好,但它的工作方式不同。现在它没有正确收取附加费。

import javax.swing.JOptionPane;
import java.text.DecimalFormat;

public class Assignment3 
{
 public static void main(String[] args)
 {
   double width;
   double length;
   double height;
   double weight;
   double totalDimen;         // (width * length * height) + weight
   double shipping;
   double totalCost;
   double cost12 = 12;
   double cost14 = 14;
   double cost17 = 17;
   double cost21 = 21;
   double cost33 = 33;
   double cost105 = 105;
   double surcharge;
   int zipCode;               //gets the zip code
   char zipCode2;

   String input;

   //changing the decimal format
   DecimalFormat formatter = new DecimalFormat("#0.00");


   //getting weight
   input = JOptionPane.showInputDialog("Enter the total weight");
   weight = Double.parseDouble(input);

   //getting height
   input = JOptionPane.showInputDialog("Enter the total height");
   height = Double.parseDouble(input);

   //getting length
   input = JOptionPane.showInputDialog("Enter the total length");
   length = Double.parseDouble(input);

   //getting width
   input = JOptionPane.showInputDialog("Enter the total width");
   width = Double.parseDouble(input);

   //getting zipCode
   input = JOptionPane.showInputDialog("Enter Zip Code");
   zipCode = Integer.parseInt(input);
   zipCode = (char)zipCode;

    //getting zipCode - now gives zipCode a CHAR value, which I can use to compare.
   zipCode2 = ((String)JOptionPane.showInputDialog("Re-Type Zip Code")).charAt(0);
   JOptionPane.showMessageDialog(null, "Zip Code: " + zipCode2);

//===================================================================================   

   //Display weight, height, lenth, width |||| DEBUG
   JOptionPane.showMessageDialog(null, "Here is your input" + "\n" + weight + "\n" + width + "\n" + height + "\n" + length + "\n" + "Zip Code: " +zipCode);

   //getting total dimensions
   totalDimen = (length * width * height) + weight;
   JOptionPane.showMessageDialog(null, "Total Dimensions:\n" + totalDimen); //DEBUG
//====================================================================================   

   //if weight is <= 5 pounds, shipping = $12.00
   if ( weight <= 5 )
   {
      shipping = 12;
      JOptionPane.showMessageDialog(null, "Shipping is: " + cost12);
   }

   //if weight is > 5 pounds and <= 15 pounds, shipping = $14.00 
   if ( weight > 5 && weight <= 15 )
   {
      shipping = 14;
      JOptionPane.showMessageDialog(null, "Shipping is: " + cost14);
   }

   //if weight is > 15 pounds and <= 34 pounds, shipping = $17.00
   else if ( weight > 15 && weight <= 34 )
   {
      shipping = 17;
      JOptionPane.showMessageDialog(null, "Shipping is: " + cost17);
   }

   //if weigth is > 34 pounds and <= 45 pounds, shipping = $21.00
   else if ( weight > 34 && weight <= 45 )
   {
      shipping = 21;
      JOptionPane.showMessageDialog(null, "Shipping is: " + cost21);
   }

   //if weigth is > 45 pounds and <= 60 pounds, shipping = $33.00
   else if ( weight > 45 && weight <= 60 )
   {
      shipping = 33;
      JOptionPane.showMessageDialog(null, "Shipping is: " + cost33);
   }

   //if weigth is > 60 pounds, shipping = $105.00
   else 
   {
      shipping = 105;
      JOptionPane.showMessageDialog(null, "Shipping is: " + cost105);
   }
//====================================================================================   

   //applying the surcharge using zip code
   if ( zipCode2 == 4 )
   {
      surcharge = (shipping * .05);
      JOptionPane.showMessageDialog(null, "Your surcharge cost at 5% is: $" + surcharge);
   }
   else if ( zipCode2 == 9 )
   {
      surcharge = (shipping * .09);
      JOptionPane.showMessageDialog(null, "Your surcharge cost at 9% is: $" + surcharge);
   }
   else
   {
      surcharge = (shipping * .14);
      JOptionPane.showMessageDialog(null, "Your surcharge cost at 14% is: $" + surcharge);
   }


//====================================================================================   

   //if the zip code is even, + 2% additional surcharge fee 


 }
}

3 个答案:

答案 0 :(得分:3)

您需要使用值初始化局部变量double shipping;。在您的代码中,如果为shipping分配了值的所有条件都不成立,并且您达到使用surcharge值计算shipping的点,那么{{1没有值,因为它是一个局部变量,编译器会确保在被访问之前分配它。

指定一个值:shipping

答案 1 :(得分:0)

从初始代码中您只需更改

double shipping = 0; 

您不能在不初始化

的情况下使用局部变量

答案 2 :(得分:0)

尝试修改“附加费”中的比较,如果/ else阻止比较输入的值与字符值(现在你正在比较一个int值)。

    // applying the surcharge using zip code
    if (zipCode2 == '4') {
        surcharge = (shipping * .05);
        JOptionPane.showMessageDialog(null, "Your surcharge cost at 5% is: $" + surcharge);
    } else if (zipCode2 == '9') {
        surcharge = (shipping * .09);
        JOptionPane.showMessageDialog(null, "Your surcharge cost at 9% is: $" + surcharge);
    } else {
        surcharge = (shipping * .14);
        JOptionPane.showMessageDialog(null, "Your surcharge cost at 14% is: $" + surcharge);
    }