我的二传手和吸气者出了什么问题?

时间:2015-10-18 15:35:53

标签: java getter-setter

我是java的新手,我的代码似乎不起作用。求助。
我有这个班级帐号

public class Account {
    private String name;
    private double balance;


    Account(){
        name = null;
        balance = 0;
    }

    Account(String name, double balance){
        setName(name);
//        this.name = name;
        setBalance(balance);
//        this.balance = balance;
        System.out.println(this.name + " " + this.balance);
    }

    private void setName(String name){
        if (name == null){
            System.out.println("Invalid Name");
        }
        else this.name = name;
    }

    private void setBalance(double balance) {
        if (balance < 0){
            System.out.println("Balance should not be negative");
        }
        else this.balance = balance;
    }

    private String getName() {
        return name;
    }

    private double getBalance() {
        return balance;
    }

    public double withdraw(double amount){
        if (amount < this.balance){

     this.balance = balance - amount;
        }
        else if (amount > this.balance) {
            System.out.println("Balance is less than Withdrawal Amount.\nCheck balance and try again");
        }
        else;   
        System.out.println(this.name + " " + this.balance);
        return this.balance; 
        }   
}


我的主要课程中有这个

Account MySavingsAccount = new Account("myName", 1000);
        MySavingsAccount.withdraw(600);


怎么没有打印出正确的平衡?

0 个答案:

没有答案