2016-08-25 2 views
-2

나는 숙제를위한 매우 기본적인 은행 계좌 프로그램을 만들고 논리 오류가 계속 발생합니다. 예금, 인출 및이자 추가 후 프로그램이 총 잔액을주는 대신 입금액을 출력합니다. 감사합니다. 감사합니다!은행 계좌 프로그램 논리 오류

public class BankAccount 
{ 

    public BankAccount(double initBalance, double initInterest) 
    { 
     balance = 0; 
     interest = 0; 
    } 

    public void deposit(double amtDep) 
    { 
     balance = balance + amtDep; 
    } 

    public void withdraw(double amtWd) 
    { 
     balance = balance - amtWd; 
    } 

    public void addInterest() 
    { 
     balance = balance + balance * interest; 
    } 

    public double checkBal() 
    { 
     return balance; 
    } 

    private double balance; 
    private double interest; 
} 

테스트 클래스

public class BankTester 
{ 

    public static void main(String[] args) 
    { 
     BankAccount account1 = new BankAccount(500, .01); 
     account1.deposit(100); 
     account1.withdraw(50); 
     account1.addInterest(); 
     System.out.println(account1.checkBal()); 
     //Outputs 50 instead of 555.5 
    } 

} 
+1

변수를 올바르게 초기화하지 않았습니다. 당신은'balance = initBalance; 관심 = initInterest'. –

+1

설명이없는 아래 투표는 도움이되지 않습니다. 또한 새로운 사용자가 도움이나 조언을 구하고 찾는 것을 막을 수 있습니다. 나는 새로운 사용자의 다운 투표 질문을 가능한 한 피해야한다고 생각합니다. 나는 반대 의견을 추천한다 : 하향 투표하지 않고 설명. – c0der

답변

4

나는 믿는다 문제가 생성자에 있습니다.

public BankAccount(double initBalance, double initInterest) 
{ 
    balance = 0; // try balance = initBalance 
    interest = 0; // try interest = initInterest 
} 
4

생성자가

public BankAccount(double initBalance, double initInterest) 
    { 
     balance = initBalance; 
     interest = initInterest; 
    } 

과 같이 인스턴스 변수로 생성자에 전달하는 값을 할당되지 않은 변경

2

기본적으로 균형 및 관심을 위해 값을 0으로 할당하는 대신 생성자 매개 변수를 할당합니다. 아래 코드를 대체하십시오.

public BankAccount(double initBalance, double initInterest) 
{ 
    balance = 0; 
    interest = 0; 
} 

public BankAccount(double initBalance, double initInterest) 
{ 
    this.balance = initBalance; 
    this.interest = initInterest; 
} 
관련 문제