簡體   English   中英

Dr Java代碼錯誤

[英]Dr Java error on code

就是說我的局部變量newaccbalance可能尚未初始化。 我知道我宣布為雙重。 請幫助

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

  }
}

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

double newaccbalance; 聲明變量。

newaccbalance = 42; 正在初始化變量。

您的代碼中的問題是,編譯器無法保證您的任何if語句都為true,因此newaccbalance可能未初始化。

我建議兩件事:

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

其次,更改if語句的結構,並使用if-else-if,如下所示:

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!
}

您正在聲明變量。 您需要初始化變量。

聲明是創建變量的地方:

double newaccbalance;

初始化是為變量分配值的地方:

newaccbalance = 0;

因此,您需要做的是:

double newaccbalance = 0.0;

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

這就是為什么該消息說它可能沒有初始化的原因。

它沒有初始化。 當您聲明它時,請嘗試使用double newaccbalance = 0.0;

我認為問題在於newaccbalance僅是有條件地設置(在if語句中),因此永遠不能保證將其設置為一個值。

初始化和聲明是兩件事。

我不認為您會收到錯誤,而是警告。
無論如何,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堅持不懈的原因。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM