2012-03-28 4 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)); 

     } 
    } 

당신이 balance 필드를 정의하지만, 당신이 static 방법 depositwithdraw I에서 액세스 할 수 있다는 사실에가는 당신은 표시하지 않은
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

그것이 그 자체로 정적 변수라고 추측한다면,

private static double balance; 

이제 static은 무엇을 의미합니까? 그 점을 이해하면 프로그램의 오류가 무엇인지 알 수 있으며, 한 객체에서 그것을 변경하면 왜 모든 변수가 변경됩니까

+0

좋아요, 변수를 어떻게 정의했는지 보여 줬습니다. 정적은 무엇을 의미합니까? – Jake

+0

그래서 * 정적 *입니다. 놀라운 일은 아닙니다. '필드'가 '정적'이라는 것은 무엇을 의미합니까? –

+1

하하! 훌륭한. 정말 고마워! – Jake

관련 문제