变量“total”可能尚未初始化?

时间:2015-02-12 22:54:30

标签: java

当我尝试编译时,它一直给我并且错误地说“total”变量没有被初始化。我知道错误发生在

的最后一行代码中
System.out.println(("The charges are ") + money.format(total) + "\n");

这是我的完整代码。我试着尽可能地格式化它。

import java.util.Scanner;

import java.text.DecimalFormat;

公共类互联网

{

public static void main(String[] args)
{

    String packageType;                        //string to get type of package bought
    char packageLetter;                        //char of converted string
    final double PACKAGE_A_PRICE = 9.95;       //Package A base price
    final double PACKAGE_A_HOURS_PRICE = 2.25; //Package A additional hours price
    final int PACKAGE_A_MIN_HOURS = 10;        //Package A minimun hours
    final double PACKAGE_B_PRICE = 14.95;      //Package B base price
    final double PACKAGE_B_HOURS_PRICE = 1.10; //Package B additional hours price
    final int PACKAGE_B_MIN_HOURS = 20;        //Package B minimun hours
    final double PACKAGE_C_PRICE = 22.95;      //Package C base price
    double hoursUsed;                          //variable for number of hours used
    final int HOURS_MIN = 0;                   //minimum hours of service use
    final double HOURS_MAX = 744;              //maximun hours of service use
    double overusedHours;                      //variable for additional hours
    double total;                              //variable for the total price
    DecimalFormat money = new DecimalFormat("$0.00"); //US currency format

    //read input
    Scanner keyboard = new Scanner(System.in);

    //get package type, convert to char, then captitalize it
    System.out.print("Which plan <A, B, or C>? ");
    packageType = keyboard.nextLine();
    packageLetter = Character.toUpperCase(packageType.charAt(0));

    //case switch to check if package input is valid
    switch (packageLetter)
    {

        case 'A':
            //number of hours & check if input is valid
            System.out.print("How many hours? ");
            hoursUsed = keyboard.nextDouble();
            if (hoursUsed < 0 || hoursUsed > 744)
                System.out.printf("Hours must be between %d and %.1f. You entered %.1f.\nProgram ending\n",HOURS_MIN,
                    HOURS_MAX, hoursUsed);

            //calculate total
            overusedHours = hoursUsed - PACKAGE_A_MIN_HOURS;
            if (overusedHours < 0)
                overusedHours = 0;
            total = PACKAGE_A_PRICE + (overusedHours * PACKAGE_A_HOURS_PRICE);
            break;

        case 'B':
            //number of hours & check if input is valid
            System.out.print("How many hours? ");
            hoursUsed = keyboard.nextDouble();
            if (hoursUsed < 0 || hoursUsed > 744)
                System.out.printf("Hours must be between %d and %.1f. You entered %.1f.\nProgram ending\n",HOURS_MIN,
                HOURS_MAX, hoursUsed);

            //calculate total
            overusedHours = hoursUsed - PACKAGE_B_MIN_HOURS;
            if (overusedHours < 0)
                overusedHours = 0;
            total = PACKAGE_B_PRICE + (overusedHours * PACKAGE_B_HOURS_PRICE);
            break;

        case 'C':
            total = PACKAGE_C_PRICE;
            break;

        default:
            System.out.printf("Package must be A, B, or C. You entered %s.\n",packageType +
                "\nProgramming ending");


    }
    System.out.println(("The charges are ") + money.format(total) + "\n");
}

}

2 个答案:

答案 0 :(得分:0)

这条信息很简单。简而言之,如果switch-case函数转向默认行为,则变量total可能永远不会获得值。 (含义packageLetter不是A,B或C.)

当代码稍后尝试并访问变量时,你会得到这个。

一种解决方案是在声明时将变量设置为0。

double total = 0;

答案 1 :(得分:0)

它可能尚未初始化,因为它可能没有达到条件。只需在声明的位置为其分配默认值0并解决问题