2013-10-12 43 views
0

這是說我的本地變量newaccbalance可能尚未初始化。我知道我宣佈它是一個雙。請幫助Dr Java代碼錯誤

import java.util.*; 

public class Pg244Problem12 { 

    public static void main(String[] args) 
    { 

    int accnum, minbalance, currentbalance; 
    int acctype; 
    double newaccbalance; 

    Scanner console = new Scanner(System.in); 



    System.out.println("Enter the customer's account number:"); 
    accnum = console.nextInt(); 
    System.out.println("Enter the customer's account type by using the number 1 for Checking or 2 for Savings:"); 
    acctype = console.nextInt(); 
    System.out.println("Enter the minimum balance the customer's account can have:"); 
    minbalance = console.nextInt(); 
    System.out.println("Enter the current balance of the customer's account:"); 
    currentbalance = console.nextInt(); 



    // Checkings 
    if(acctype == 1 && currentbalance >= (minbalance+5000)){ 
    newaccbalance = ((currentbalance*.05)*(1/12)); 
    } 
    if (acctype == 1 && currentbalance >= minbalance && currentbalance < (minbalance+5000)){ 
    newaccbalance = ((currentbalance*.03)*(1/12)); 
    } 
    if (acctype == 1 && currentbalance < minbalance){ 
    newaccbalance = (currentbalance-25); 
    } 

    // Savings 
    if (acctype == 2 && currentbalance >= minbalance){ 
     newaccbalance = ((currentbalance*.04)*(1/12)); 
    } 
    if (acctype == 2 && currentbalance < minbalance){ 
     newaccbalance = (currentbalance - 10); 
    } 



    System.out.println("The account number is: "+ accnum); 
    System.out.println("The account type is: "+ acctype); 
    System.out.println("The current balance is: "+ currentbalance); 
    System.out.println("The new account balance is: "+ newaccbalance); 

    } 
} 
+0

該代碼不可讀。請編輯:) – tianz

+0

@CoderTian我剛剛做到了!您還可以提交需要改進的問題的編輯建議。 –

+0

這是我第一次使用這個網站,我不是很擅長它 – user2874840

回答

3

首先,聲明和初始化不是一回事。

double newaccbalance;聲明變量。

newaccbalance = 42;初始化變量。

在你的代碼的問題是,編譯器不能保證您的if語句將是真實的,因此有可能newaccbalance要留給初始化。

我建議兩兩件事:

首先,初始化變量的默認值,double newaccbalance = 0;都將聲明和初始化變量。

其次,改變你的if語句的結構,還可以使用的if-else-如果是這樣的:

if (acctype == 1) { 
    // For these if statements, acctype is 1 so we don't need to check that again 
    if(currentbalance >= (minbalance+5000)){ 
     newaccbalance = ((currentbalance*.05)*(1/12)); 
    } 
    else if (currentbalance >= minbalance) { 
     // && currentbalance < (minbalance+5000) will be true because the above if-statement is **not** true 
     newaccbalance = ((currentbalance*.03)*(1/12)); 
    } 
    else { 
     // if (acctype == 1 && currentbalance < minbalance) would always be true here 
     newaccbalance = (currentbalance-25); 
    } 
} 
else if (acctype == 2){ 
    // Savings 
    if (currentbalance >= minbalance) { 
      newaccbalance = ((currentbalance*.04)*(1/12)); 
    } 
    else { // currentbalance < minbalance) is always true here 
      newaccbalance = (currentbalance - 10); 
    } 
} 
else { 
    // acctype is neither 1 or 2, what should we do now? RuntimeError, Catastrophic failure, the monsters are coming! We're screwed! 
} 
+0

那麼應該怎樣編碼? – user2874840

+0

我該怎麼辦? – user2874840

+0

當你聲明它時給你的變量一些默認值。 – ArniDat

1

您是聲明您的變量。你需要初始化你的變量

聲明是創建變量:

double newaccbalance; 

初始化是你分配一個變量的值:

newaccbalance = 0; 

所以,你需要做的是:

double newaccbalance = 0.0; 
0

您的所有作業都包含在if控制結構中。如果沒有的條件評估爲true,變量將保持未分配狀態。作爲local變量,它也不會獲得默認值。

那就是爲什麼有消息稱,這可以將未初始化。

0

它沒有被初始化。當你聲明它嘗試double newaccbalance = 0.0;

我認爲問題是newaccbalance只是有條件地設置(在if語句中),所以它永遠不能保證被設置爲一個值。

初始化和聲明是兩回事。

0

我不認爲你得到一個錯誤,但一個警告。
無論如何,Java是正確的。
您的變量newaccbalance可能尚未初始化。

你已經聲明它爲double,但你只在if語句中賦值。
Java不知道這些if語句是否覆蓋了所有可能的情況,因此會警告您實際上可能未分配的內容爲newaccbalance

請注意,Java不會爲未定義的變量分配零值。
你必須自己做。

更改頂部聲明:該

double newaccbalance = 0; //Or whatever default value you want. 

要麼或增加額外的else背後的最後一個像這樣:

else if (acctype == 2 && currentbalance < minbalance){ 
    newaccbalance = (currentbalance - 10); 
} 
else { newaccbalance = 0; } 

這將確保該編譯器的滿意度newaccbalance已定義價值,而不是隨機的。
你應該始終確保情況是這樣,並且kuddo聽取警告並採取行動。
未定義的變量可能是一個很難追蹤錯誤的來源,因爲除了1%的情況外,通常該值會產生一些合理的值。因爲每次運行的代碼都不同,所以很難重現錯誤,更不用說診斷它了。

這就是Java堅持的原因。

+0

我做到了,但它仍然沒有運行該變量通過if語句並返回一個新值,它只是表示它仍爲零 – user2874840