2016-09-07 145 views
0

我已經在這個程序上工作了幾天,並且今晚在午夜的時候到了。我不能爲我的生活弄清楚爲什麼我總是得到一個「局部變量無法初始化」的錯誤。這是我的第一個編程課,我不太瞭解它。如果有人能夠通過解釋修補程序來幫助我解決問題,併爲什麼這個錯誤會持續發生,那將會很棒Eclipse,局部變量無法初始化

我已經把「**」放在錯誤所在的位置(靠近代碼的末尾)。任何幫助將是偉大的!提前致謝。

/*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)); 

    } 

} 
+0

您應該添加標籤「java」 – Rodney

+0

請閱讀Java命名約定。長遠來看,你會從中受益。你所有的變量都被錯誤地命名。此外,正確使用縮進 - 這也能爲你自己的利益服務。 – ppeterka

回答

0

,如果他們不肯定的回答這個問題你變量INFCSCFee未初始化「你服用任何INF 120或260 CSC?(Y/N)」。如果一個變量可能沒有被初始化,Eclipse將不會讓你運行該程序。在你的代碼的頂部,你必須

int INFCSCFee; 

int INFCSCFee = 0; 

更換或初始化變量其他地方或其他一些價值。

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 

不能使用在方法中聲明一個變量,除非編譯器可以gaurantee已經初始化beore你我們吧。這將允許:

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 

最後,你可以在聲明時initalise您的變量:無論在哪裏使用X

int x = 0; 

現在,它被嚴格保密,所以你不會收到編譯錯誤。這不一定是好事,因爲編譯器錯誤實際上可能會向您顯示代碼中的錯誤,並且您通過給該變量設置默認值來抑制它。