2012-03-28 40 views
1

當我使用存款和取款金額更新數組中某個記錄的餘額時,該記錄的餘額隨着數組中記錄的餘額而變化。在多個陣列中調整的值

如何解決?

private String name; 
private int pin; 
private int account; 
private static double balance; 

public void setBalance(double amount) 
{ 
    balance = amount; 
} 

public static void deposit(double aDeposit) 
{ 
balance = balance + aDeposit; 
} 
public static void withdraw(double aWithdraw) 
{ 
    if 
    (balance >= aWithdraw) 
     balance = balance - aWithdraw; 
    else if 
    (balance < aWithdraw) 
     System.out.println("Cannot withdarw amount."); 
} 

public double getBalance() 
{ 
    return balance; 
} 
public boolean equal(CustomerRecord otherObject) 
{ 
    return (name.equalsIgnoreCase(otherObject.name) && 
      (pin == otherObject.pin) && 
      (account == otherObject.account) && 
      (balance == otherObject.balance)); 

     } 
    } 

do{ 


     System.out.println("Enter the name"); 
     String aName; 
     Scanner keyboard = new Scanner(System.in); 
     aName = keyboard.nextLine(); 

     System.out.println("Enter the pin"); 
     int aPin; 
     aPin = keyboard.nextInt(); 


      for 

      (int i = 0; i < index; i++) { 
       CustomerRecord record = anotherArray[i]; 
       if 
       ((record.getName().equalsIgnoreCase(aName)) && (record.getPin() == (aPin))) 

      { 
       System.out.println(record); 

      System.out.println("Enter the amount you wish to Deposit"); 
      double aDeposit; 
      aDeposit = keyboard.nextDouble(); 
      CustomerRecord.deposit(aDeposit); 

      System.out.println("Enter the amount you wish to withdraw"); 
      double aWithdraw; 
      aWithdraw = keyboard.nextDouble(); 
      CustomerRecord.withdraw(aWithdraw); 


      record.getBalance(); 

    } 
      } 

     System.out.println("\nAnother Transaction? (y for yes) (n for no)"); 
     repeat = keyboard.next().charAt(0); 

    } 
    while 
     (

     repeat == 'y' || repeat == 'Y') ; 

     //Print the records on screen 

    { for (int i = 0; i < index; i++) 
     System.out.print(anotherArray[i]); 
    } 

回答

3

您還沒有顯示在其中定義balance場,而是由事實,你是能夠從static方法depositwithdraw我訪問它去會猜測它本身就是一個靜態變量,說

private static double balance; 

現在,static是什麼意思?如果你知道這一點,你會知道你的程序中有什麼錯誤,以及爲什麼改變它在一個對象中改變它在所有的

+0

好吧,我已經告訴你我是如何定義我的變量。靜態是什麼意思? – Jake 2012-03-28 01:49:10

+0

所以它*是*'靜態'。這並不奇怪。 「field」爲「static」意味着什麼? – 2012-03-28 01:50:56

+1

哈哈!輝煌。非常感謝! – Jake 2012-03-28 01:55:42