5

我对以下语法有一些问题。

我目前正在学习 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);

}
}

再次请原谅我的代码中的任何错误。我还在学习它的语法。

4

7 回答 7

6

在 if 语句if(userInput == withdraw)中,您尝试比较String对象。

在 Java 中比较String对象时使用equals方法而不是比较运算符==

if(userInput.equals(withdraw))

代码中有几个实例使用change these to use来比较String对象。==equals

此外,当使用条件块时,最好用大括号包围块{}

if(true){

}
于 2013-05-13T10:22:06.443 回答
3

您不使用方括号,因此仅执行 if 语句后的第一行。此外,应该使用 比较字符串.equals(otherString)。像这样:

if(userInput.equals(withdraw))
    System.out.println("Enter amount to withdraw: "); //Only executed if userInput == withdraw
    amount = in.nextDouble(); //Always executed

if(userInput.equals(withdraw)) {
    System.out.println("Enter amount to withdraw: "); 
    amount = in.nextDouble(); 
    //All executed
}

做这个:

if(userInput.equals(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.equals(deposit)) {
     System.out.println("Enter amount to deposit: ");
     amount = in.nextDouble();
     credit(currentBalance, amount);
     System.out.println("Your new balance is: " + currentBalance);
}

请注意,如果您的提款金额超过当前余额,您将收到“警告消息”,但您的提款将继续。因此,您最终会得到一笔负数的钱。如果您不想这样做,则必须相应地进行更改。但是,通过这种方式,它显示了使用括号(或不使用括号)如何产生不同的效果。

于 2013-05-13T10:20:45.453 回答
2

在这些方法上:

public static double debit(double currentBalance, double amount) {
    currentBalance -= amount;
    return currentBalance;
}

public static double credit(double currentBalance, double amount) {
    currentBalance += amount;
    return currentBalance;
}

函数的输入实际上不应该包括当前余额,对象已经知道当前余额是什么(它保存在对象 currentBalance 字段中,正如已经指出的那样,它不应该是静态的)。

想象一个真正的提款机,它的行为是这样的:

Whats my current balance:
£100
CreditAccount("I promise my current balance is £1 Million, it really is", £10):
Balance:£1,000,010

编辑:包含这样行为的代码

import java.util.*;

public class Account {

private int accountNumber;
private double currentBalance; //balance kept track of internally

// ***** CONSTRUCTOR *****//

    public Account(int accountNumber, double currentBalance) {
        this.accountNumber = accountNumber;
        this.currentBalance = currentBalance;
    }


    public int getAccountNumber() {
        return accountNumber;
    }

    public double getcurrentBalance() {
        return currentBalance;
    }

    public boolean debit(double amount) {
        //we just refer to the objects fields and they are changed

        if (currentBalance<amount){
            return false; //transaction rejected
        }else{
            currentBalance -= amount;
            return true;
            //transaction approaved and occured
        }

        //Note how I directly change currentBalance, there is no need to have it as either an input or an output

    }

    public void credit( double amount) {
        //credits will always go through, no need for return boolean
        currentBalance += amount;

        //Note how I directly change currentBalance, there is no need to have it as either an input or an output
    }

    public static void main(String [] args){
        Account acc=new Account(1234,1000);

        acc.credit(100);

        System.out.println("Current ballance is " + acc.getcurrentBalance());

        boolean success=acc.debit(900); //there is enough funds, will succeed

        System.out.println("Current ballance is " + acc.getcurrentBalance());
        System.out.println("Transaction succeeded: " +  success);


        success=acc.debit(900); //will fail as not enough funds

        System.out.println("Current ballance is " + acc.getcurrentBalance());
        System.out.println("Transaction succeeded: " +  success);


    }
}

我没有打扰使用键入的输入,因为您似乎掌握了这一点

于 2013-05-13T10:42:23.747 回答
2
if (userInput == deposit)

应该

if (userInput.equals(deposit))

取款也一样。

于 2013-05-13T10:23:37.203 回答
1

使用 equals() 方法代替 == 比较 Objetcs 的相等性而不是值

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.equals(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 .equals(deposit))
                System.out.println("Enter amount to deposit: ");
                    amount = in.nextDouble();
                    credit(currentBalance, amount);

        System.out.println("Your new balance is: " + currentBalance);

}
}
于 2013-05-13T10:25:17.570 回答
1

对于借记和贷记方法:

public static boolean debit(double currentBalance, double amount) {
   currentBalance -= amount;
   if<currentBalance < 0){
       return false
   }
   return true;
}

public static boolean credit(double currentBalance, double amount) {
   currentBalance += amount;
   if<currentBalance > 0){
       return false
   }
   return true;
}

现在我想我把布尔值弄混了。关于每种方法返回什么的描述有点不清楚。

于 2013-05-13T10:29:53.357 回答
1

如果没有 '{' 和 '}',if 语句后的第一行只会作为该语句的一部分执行。此外,您的if (userInput == deposit)块没有正确缩进,它不应该在if (userInput == withdraw). 字符串比较应该使用userInput.equals(withdraw)

于 2013-05-13T10:22:48.647 回答