我是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);
怎么没有打印出正确的平衡?