[英]Eclipse, The local variable cannot be initialized
我已經在這個程序上工作了幾天,並且應該在今晚的午夜。 我一輩子都無法弄清楚為什么我總是收到“無法初始化局部變量”錯誤的原因。 這是我的第一門編碼課,我不太了解。 如果有人可以通過解釋修復程序來幫助我,以及為什么繼續發生此錯誤,那將是很好的。
我將“ **”放在錯誤所在的位置(接近代碼結尾)。 任何幫助將是巨大的! 提前致謝。
/*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));
}
}
如果變量INFCSCFee對問題“您是否服用INF 120或CSC 260? 如果變量尚未初始化,Eclipse將不允許您運行程序。 在代碼的頂部
int INFCSCFee;
替換為
int INFCSCFee = 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,都將確保它被初始化,因此不會出現編譯錯誤。 這不一定是一件好事,因為該編譯器錯誤實際上可能會向您顯示代碼中的錯誤,並且您通過為變量提供默認值來抑制了它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.