Eclipse,局部变量无法初始化

时间:2016-09-07 22:41:20

标签: java eclipse

我已经在这个项目上工作了几天,今晚到了午夜。我不能为我的生活找出为什么我一直得到一个"局部变量无法初始化"错误。这是我的第一个编码类,我不太了解它。如果有人可以通过解释修复来帮助我,以及为什么这个错误会继续发生,这将是非常好的。

我已经把#34; **"错误的位置(接近代码的末尾)。任何帮助都会很棒!提前谢谢。

/*This program will determine how much the students 
tuition and fees are based on location and classes. It will return the
total tuition, fees, and combined total */
import java.text.DecimalFormat;
import java.util.*;

public class Project2 {

public static void main(String[] args) {
    Scanner in=new Scanner(System.in);

    //Declaring variables and decimal format
    int TotalHours;
    int Price;
    int CreditCharge;
    int CITFee;
    int OnlineFee;
    int INFCSCFee;
    int TotalTuition;
    int TotalFee;
    int TotalCombined;
    DecimalFormat df = new DecimalFormat("$#,###");


    //Getting the students First name, Last name, and Date
    System.out.print("Enter your first name: ");
    String FirstName = in.nextLine();
    System.out.print("Enter your last name: ");
    String LastName = in.nextLine();
    Date d = new Date ( );


    //Getting the state of residency. If in Ohio, asking the user if they are Metro
    System.out.print("Enter your State of residency as a 2-letter abbreviation: ");
    String State = (in.next().toLowerCase());

    if (State.equals ("oh") || State.equals("OH")){
        System.out.print( "Are you a Cincinnati resident? (Y/N) ");
        String Metro = in.next();    
        if (Metro.equals ("y")) Price = 567;    
        }
    else Price = 750;

    if (State.equals ("ky") ){ Price = 375; 
        }
        else if (State.equals ("in")){Price = 375;  
        }
        else {Price = 750;
        }       

    //Getting the number of credit hours the student is taking
    System.out.print("Enter the total credit hours for the upcoming semester: ");
     TotalHours = in.nextInt();

    if (TotalHours <= 12) CreditCharge = (TotalHours * Price);

    else {CreditCharge = (Price * 12);
    }    

    //Getting the number of CIT hours the student is taken
    System.out.print("Enter the total of CIT credits you are taking: ");
    int TotalCITHours = (int) in.nextInt();
    CITFee = (TotalCITHours * 40);

    //Getting the number of online credit hours the student is taken
    System.out.print("Enter the total number on-line credit hours you are         taking: ");
    int OnLine = (int) in.nextInt();
    OnlineFee = (OnLine * CITFee * 35);

    //Seeing if the student is taken either INF 120 or CSC 260 
    System.out.print("Are you taking either INF 120 or CSC 260? (Y/N) ");
    String INFCSC = in.next().toLowerCase();
    if (INFCSC.equals ("y")) INFCSCFee = (char) (CITFee * OnlineFee + 60);

    //Calculating the tuition, fees, and total combined.
    **    TotalTuition = CreditCharge;
    **    TotalFee = INFCSCFee;
    **    TotalCombined = TotalTuition + INFCSCFee;       

    //Tuition Statement for FirstName, LastName, Date
    System.out.println("\nTuition Statement for " + FirstName + LastName);
    System.out.println(d);
    System.out.println("Tuition: " + df.format (TotalTuition) );
    System.out.println("Fees: " + df.format(TotalFee));
    System.out.println("Total: " + df.format(TotalCombined));

    }

}

2 个答案:

答案 0 :(得分:0)

如果你的变量INFCSCFee没有回答“是你服用INF 120还是CSC 260?(Y / N)”这个问题,那么它就没有被初始化。如果变量可能尚未初始化,Eclipse将不允许您运行该程序。在您的代码顶部

int INFCSCFee;

替换为

int INFCSCFee = 0;

或在其他地方或其他值初始化变量。

答案 1 :(得分:0)

这是错误

    if (INFCSC.equals ("y")) INFCSCFee = (char) (CITFee * OnlineFee + 60);

这是唯一可以初始化INFCSCFee的地方。因此,有可能它在使用时尚未初始化。更一般地说,这是不允许的:

int x; // declare x - not yet initialised
if (someCondition)
   x = 3; // initialise to 3
// if somecondition was false, x is still unitialised
System.out.println("x is "+x); // Error

你不能使用在方法中声明的变量,除非编译器可以保证它已被初始化而不是你。这是允许的:

int x; // declare x - not yet initialised
if (someCondition)
   x = 3;
else if (somethingElse)
   x = 4;
else
   x = 5;

// For all outcomes, x has been initalised, so it is safe to use
System.out.println("x is "+x);

这也是允许的:

int x; // declare x - not yet initialised
if (someCondition)
   x = 3;
else
   return;

// Although someCondition may have been false, if we reach this line of
// code, it is because someCondition was true and therefore x was initialised
System.out.println("x is "+x); // will print x is 3

最后,您可以在申报时启动您的变量:

int x = 0;

现在,无论在哪里使用x,都可以获得初学者的帮助,因此您不会遇到编译错误。这不一定是好事,因为编译器错误实际上可能会向您显示代码中的错误,并且您通过为变量提供默认值来抑制它。