我对以下语法有一些问题。
我目前正在学习 Java,并且一直在通过过去的试卷来帮助建立我的 Java 知识。
这是问题:
编写一个 Account 类,其中包含帐号和当前余额的实例变量。实现构造函数和方法 getAccountNumber()、getBalance()、debit(double amount) 和 credit(double amount)。在您的借记卡和贷记卡实现中,检查指定金额是否为正数,以及在借记卡方法中不会导致透支。在这些情况下返回 false。否则,更新余额。
我试图这样做但是,我没有为借方和贷方方法实现布尔函数。我只是想先构建程序并尝试让它工作。之后我打算看看这个,因为我不确定如何返回 true 或 false,同时还试图从上述方法返回一个金额。
请原谅我的代码中的任何错误,因为我仍在学习 Java。
我可以运行我的代码,但是当我输入存款时,它似乎无法正常工作,我将不胜感激。
这是我的代码:
import java.util.*;
public class Account {
private int accountNumber;
private static double currentBalance;
private static double debit;
// ***** CONSTRUCTOR *****//
public Account(double currentBalance, int accountNumber) {
accountNumber = 12345;
currentBalance = 10000.00;
}
public int getAccountNumber(int accountNumber) {
this.accountNumber = accountNumber;
return accountNumber;
}
public double getcurrentBalance(double currentBalance) {
this.currentBalance = currentBalance;
return currentBalance;
}
public static double debit(double currentBalance, double amount) {
currentBalance -= amount;
return currentBalance;
}
public static double credit(double currentBalance, double amount) {
currentBalance += amount;
return currentBalance;
}
public static void main(String [] args){
String withdraw = "Withdraw";
String deposit = "Deposit";
double amount;
Scanner in = new Scanner(System.in);
System.out.println("Are you withdrawing or depositing? ");
String userInput = in.nextLine();
if(userInput == withdraw)
System.out.println("Enter amount to withdraw: ");
amount = in.nextDouble();
if(amount > currentBalance)
System.out.println("You have exceeded your amount.");
debit(currentBalance, amount);
System.out.println("Your new balance is: " + currentBalance);
if (userInput == deposit)
System.out.println("Enter amount to deposit: ");
amount = in.nextDouble();
credit(currentBalance, amount);
System.out.println("Your new balance is: " + currentBalance);
}
}
再次请原谅我的代码中的任何错误。我还在学习它的语法。