Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/397.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/mercurial/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 如何返回并设置为零?_Java - Fatal编程技术网

Java 如何返回并设置为零?

Java 如何返回并设置为零?,java,Java,我想问一个问题。如果金额大于余额,我必须返回全部余额并将余额设置为零。。我该怎么做?我尝试了很多不同的方法来解决这个问题,但都没有成功。谢谢你的帮助 public class BankAccount { double balance; BankAccount(double openingBalance){ balance=openingBalance; } public double getBalance(){ retur

我想问一个问题。如果金额大于余额,我必须返回全部余额并将余额设置为零。。我该怎么做?我尝试了很多不同的方法来解决这个问题,但都没有成功。谢谢你的帮助

public class BankAccount {
    double balance;

    BankAccount(double openingBalance){
        balance=openingBalance;
    }
        public double getBalance(){
        return balance; 
    }
    public void deposit(double amount){
        balance += amount;
    }
    public double withdraw (double amount){ 
        if (balance > amount){
            balance -= amount;
        }else if (amount > balance || amount == balance){       
            **return balance; 
            balance = 0**
        }   
        return amount;
    }
}
驾驶员等级

public class Driver {

    static BankAccount acc3;

    public static void main (String[] args){
        BankAccount  acc3 = new BankAccount ("Alana","Neil", 5000);
        System.out.println("\nName: " +acc3.Name());
        System.out.println("Amount: $" +acc3.balance);
        acc3.deposit(100);
        System.out.println("Deposit Amount: $" +acc3.balance);
        System.out.println("Withdrawl Amount: $"+acc3.withdraw(5400));
        System.out.println("The New Balance: $" +acc3.balance);

    }
}

使用临时变量:

double tmp = balance;
balance = 0;
return tmp;

如果您只是对您的意思进行了编码(当余额不足时更改金额),这将更简单:

其他部分可以是:

}
else {
    amount = this.balance;
    this.balance = 0;
}

因此,您满足了只返回可提取余额的要求,并将剩余余额设置为零。

首先,您不需要返回金额,因为您将其传递给方法。 那你还想要什么?金额太大的错误,或者只是可以提取的金额? 对于方法:

public double withdraw (double amount){ 
    if (balance > amount){
        balance -= amount;
    }else{ //no need for second if since we check in first 
             // if amount is smaller then balance
        // if you whant to withdraw all money
        return balance = 0; // 
        // or return -1 and in balance method change if balance = -1 you are broke
        return balance = -1;
    }   
    return balance;
}
public double withdraw (double amount){ 
    if (balance > amount){
        balance -= amount;
    }else{ //no need for second if since we check in first 
             // if amount is smaller then balance
        // if you whant to withdraw all money
        return balance = 0; // 
        // or return -1 and in balance method change if balance = -1 you are broke
        return balance = -1;
    }   
    return balance;
}
public double withdraw (double amount){

     double toReturn = amount;

    if (balance > amount){
        balance -= amount;
    }
    else if (amount > balance) {
        toReturn = balance;      
        balance = 0;
    }
    else if (amount == balance){ 
        balance = 0;
    }
    return toReturn;
}