我已經在這個程序上工作了幾天,並且今晚在午夜的時候到了。我不能爲我的生活弄清楚爲什麼我總是得到一個「局部變量無法初始化」的錯誤。這是我的第一個編程課,我不太瞭解它。如果有人能夠通過解釋修補程序來幫助我解決問題,併爲什麼這個錯誤會持續發生,那將會很棒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));
}
}
您應該添加標籤「java」 – Rodney
請閱讀Java命名約定。長遠來看,你會從中受益。你所有的變量都被錯誤地命名。此外,正確使用縮進 - 這也能爲你自己的利益服務。 – ppeterka