0

私のローカル変数newaccbalanceが初期化されていない可能性があると言っています。私はそれを double として宣言したことを知っています。助けてください

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

  }
}
4

5 に答える 5

3

まず、宣言と初期化は同じではありません。

double newaccbalance;変数を宣言します。

newaccbalance = 42;変数を初期化しています。

コードの問題は、コンパイラが if ステートメントのいずれかが true であることを保証できないため、初期化されnewaccbalanceていないままになる可能性があることです。

私は2つのことを提案します:

まず、変数をデフォルト値に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!
}
于 2013-10-12T20:42:53.583 に答える
1

変数を宣言しています。変数を初期化する必要があります。

宣言は、変数を作成する場所です。

double newaccbalance;

初期化は、変数に値を割り当てる場所です。

newaccbalance = 0;

だからあなたがする必要があるのは:

double newaccbalance = 0.0;
于 2013-10-12T20:43:51.317 に答える
0

すべての割り当ては、if制御構造でラップされています。どの条件も に評価されない場合true、変数は未割り当てのままになります。変数として、localデフォルト値も取得しません。

そのため、初期化されていない可能性があるというメッセージが表示されます。

于 2013-10-12T20:44:30.250 に答える
0

初期化されていません。宣言するときは、 double newaccbalance = 0.0; を試してください。

問題は、newaccbalance が (if ステートメントで) 条件付きでのみ設定されるため、値に設定されることを保証できないことだと思います。

初期化と宣言は 2 つの異なるものです。

于 2013-10-12T20:44:32.403 に答える
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 がランダムな値ではなく、定義された値を持つことが保証されます。
これが事実であることを常に確認する必要があり、警告に注意して行動を起こすことは称賛に値します。
未定義の変数は、バグの追跡が非常に困難な原因となる可能性があります。これは、1% のケースを除いて、値が妥当な値になることがよくあるためです。コードの実行は毎回異なるため、バグを再現することはもちろん、診断することも困難です。

これが Java がしつこい理由です。

于 2013-10-12T20:45:33.377 に答える