[英]The local variable cannot be initialized
我正在處理這組代碼,但是遇到了“初始化本地變量”錯誤。 我是用Java編寫代碼的新手,我正在嘗試學習基礎知識。 我知道該變量需要初始化,但是我不知道該怎么做。 我知道我可以做到“整數價格= 0;” 但這總會返回價格為$ 0。 任何幫助將不勝感激。
public static void main(String[] args) {
Scanner in = new Scanner(System.in);
int price;
String customertype;
double bonus;
DecimalFormat df = new DecimalFormat("$#,###");
System.out.println("Are you a residential (r), commercial (c), Educational (e), or Preferred (p) customer?");
customertype = in .next().toLowerCase();
System.out.println("Please enter the number of minutes the customer used NkuTel services for the week");
double minutes = in .nextInt();
//Weekly rate of $5. 10 cents per minute over 60 mins.
if (minutes <= (0) && (minutes >= 10080)) {
System.out.println("Cannot have that amount of minutes. Please try again");
if (customertype.equals("r")) {
if (minutes > (60)) price = (int)((5 + .010) - 60);
if (minutes <= 60) price = 5;
} {}
//20 cents per minute for first 300. 15 cents per min after that
if (customertype.equals("c")) {
if (minutes <= 300) price = (int)(.20 * 300);
if (minutes >= 300) price = (int)(minutes * (.15));
if (minutes >= 300) bonus = (price * (-.30));
System.out.println("You get a bonus for being over 300 minutes!");
}
//Educational customer charged 18c per min.
if (customertype.equals("e")) {
price = (int)(.18 * minutes);
}
if (customertype.equals("p"))
if (minutes >= 500) price = (int)((minutes * .04) + 10);
if (minutes < 500) price = (int)((minutes * .06) + 10);
else
System.out.println("Error. Please enter either 'r' or 'c'");
}
/*Preferred customer pays $10 base and 6c per min.
if <500 then rate is 4c per min. */
// else{
// System.out.println("Error. Please enter either 'r' or 'c'");}
System.out.println("Your total minutes is " + minutes + ", your total bill is " + df.format(price));
}
}
您需要初始化price,編譯器了解到並非在所有情況下都初始化了您的價格(如果您的條件都不為真),請先對其進行初始化,或者將else進行初始化,然后告訴編譯器您正在對其進行初始化在所有情況下
問題是只有在客戶類型為“ r”,“ c”,“ e”或“ p”時才設置價格。 對於您的程序,這些可能是唯一有效的客戶類型。 但是編譯器不知道-如果客戶鍵入“ z”怎么辦? 在這種情況下,您將無法打印出價格,因為您從未將價格設置為任何價格。
要解決這個問題,只需聲明您的價格開始於int price = 0;
這不會導致當前代碼出現問題,因為當客戶類型有效時,您將覆蓋0
值。
更好的解決方案是設置您的代碼以處理輸入無效客戶類型的情況。 看來您嘗試使用System.out.println("Error. Please enter either 'r' or 'c'");
,但您做得不太正確。 它可能看起來像這樣:
//changed this line - it should be if minutes less than 0 or greater than 10000, not AND
if (minutes <= (0) || (minutes >= 10080)) {
System.out.println("Cannot have that amount of minutes. Please try again");
}
if (customertype.equals("r")) {
if (minutes > (60)) {
price = (int)((5 + .010) - 60);
}else {
price = 5;
}
}else if (customertype.equals("c")) {
//20 cents per minute for first 300. 15 cents per min after that
if (minutes <= 300) {
price = (int)(.20 * 300);
}
if (minutes >= 300) {
price = (int)(minutes * (.15));
bonus = (price * (-.30));
System.out.println("You get a bonus for being over 300 minutes!");
} //fixed this block to include the print only if they actually earned the bonus
}else if (customertype.equals("e")) {
//Educational customer charged 18c per min.
price = (int)(.18 * minutes);
}else if (customertype.equals("p"))
if (minutes >= 500) {
price = (int)((minutes * .04) + 10);
}
if (minutes < 500) {
price = (int)((minutes * .06) + 10);
}
}else
System.out.println("Error. Please enter either 'r' or 'c'");
}
我更正了一些其他小錯誤,但我認為某些錯誤可能仍然存在。 例如,您計算了超過300分鍾的bonus
,但您從不做任何事情。 但是,在我將代碼固定到應該可以運行的程度之后,我將讓您處理其中的一些問題。
請記住,使用大括號( {}
在你的if語句塊會更清楚都包括在什么語句if
和那些什么都沒有。 我極力建議始終對每個if語句使用花括號,即使您並非總是如此,尤其是因為您的原始代碼在該領域有些混亂。
其他答案解決了這個問題,即“價格”尚未初始化,但是由於您是“編寫代碼的新手”,因此您應該意識到,可以通過代碼的結構來隱藏此類問題,並可以通過其他兩種策略來避免這種問題。
首先,始終在塊周圍使用{},因此代碼如下:
if (minutes < 500) price = (int)((minutes * .06) + 10);
else
System.out.println("Error. Please enter either 'r' or 'c'");
會成為:
if (minutes < 500) {
price = (int)((minutes * .06) + 10);
}
} // From a prior if statement.
else {
System.out.println("Error. Please enter either 'r' or 'c'");
}
可以將一個好的IDE(例如Eclipse)配置為為您添加那些IDE。
另一個策略是一起避免if / else / if“階梯”,並使用switch構造。 請注意,在Java開關中使用字符串是一個相對較新的功能。
// Compute price based on customer type
switch (customertype) {
case "e":
// Your code here.
break;
case "c":
// Your code here.
break;
case "r":
// Your code here.
break;
case "p":
// Your code here.
break;
// ALWAYS, ALWAYS, ALWAYS use a default: case in a switch.
// ALWAYS.
default:
System.out.println("Error. Please enter either 'r' or 'c'");
break;
}
這種結構可以很清楚地說明在哪里處理客戶和未定義變量。 如果跨客戶類型進行通用處理,那么這可能不是一個好選擇,或者您可以將此類代碼放入方法中並根據需要調用它們。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.