-2

したがって、このプログラムには銀行口座があり、ユーザーが残高を変更できるようにする方法に取り組んでいます。これが私が持っているものです。

   public double modifyBalance(int accountID, double adjustment) {
     Account temp = findAccount(accountID, 0, size, lastPos);
     double currBal = temp.getBalance();    
     if (temp!= null){      
        currBal = currBal + adjustment;
     }
     else {
           System.out.println("No account found. ");
        }


     return currBal;
  }

しかし、currBalの戻りは実際のアカウントの残高を更新していません。試しtemp.getBalance() = currBal;ましたが、うまくいきませんでした。コンパイルエラーが発生しました。

OrderedVectorOfAccounts.java:95:エラー:シンボルが見つかりません
temp.getBalance = currBal;
        ^
シンボル:変数getBalance
場所:タイプアカウント
1エラーの可変温度

例:アカウントの残高が200で、200を「入金」または追加した場合、400になるはずですが、私が持っているものでは200のままです
。ありがとう!

これは私のfindAccount()です:

public Account findAccount(int accountID, int from, int to, int [] lastPos){
     if(from > to || (from+2)/2 >= size)
        return null;
     while(from <=to) {   
        if(accountID == theAccounts[(from+to)/2].getAccountNum())
           return theAccounts[(from + to)/2];
        else if (accountID>theAccounts[(from + to)/2].getAccountNum()){
          // return findAccount(accountID, (((from + to)/2)+1), to, lastPos);
           return theAccounts[accountID-1];
        }
        else if (accountID<theAccounts[(from + to)/2].getAccountNum()){
           //return findAccount(accountID, from, (((from + to)/2)-1),lastPos);
           return theAccounts[accountID-1];
        }
     }
     lastPos[0] = (from + to)/2;
     return null;
4

2 に答える 2

0

コードの関連部分を投稿していないので、あなたtemp.getBalance()が戻っている0.0か、findAccount()メソッドが空の新しいAccount()オブジェクトを返していると思います。

  public Account findAccount(yourparameters) {
      Account account = new Account();
       //do your finding logic
      return account;

アカウントが見つからない場合でも、 Account オブジェクトが返されるため、

     double currBalc= temp.getBalance(); would return 0.0  

 if (temp!= null){  // your if passes here as temp is not null    
    currBal = currBal + adjustment;   0.0+200.0
 }
 else {
       System.out.println("No account found. ");
    }

finfAccount() を更新した後に編集します

getBalance()への呼び出しが 0.0 を返している可能性があります

行の後 double currBal = temp.getBalance();に 200.0 が表示された場合、temp は null です。

于 2012-10-31T22:32:49.537 に答える
0

メソッドは変数をmodifyBalance更新するだけで、オブジェクトはそのまま残ります。これがの変数が更新されない理由です。この混乱をすべて削除して、簡単なメソッドを作成してみませんか?currBaltemptempcurrentBalancesetBalance

public void setBalance(double balance) { this.currentBalance = balance;   }

これにより、コード内のどこでも呼び出すことができます。

 Account temp = findAccount(accountID, 0, size, lastPos); 
 if (temp!= null) {   
   temp.setBalance(temp.getBalance() + adjustment); //updates temp currentBalance
 } 
 else { System.out.println("No account found. "); }
于 2012-10-31T23:12:10.180 に答える