有人可以解释为什么我的打印方法不起作用吗?

时间:2018-09-23 02:01:31

标签: java printing

我正在尝试从班级银行的printbalances方法中另一个名为getName的类中调用getBalanceAccount方法。但它不起作用,在输入客户和余额后打印为空。有人可以解释为什么以及如何解决它吗?老实说,我不知道为什么它不起作用。

这是调用它的类:

class Bank {

    ArrayList<Account> accounts = new ArrayList<>();
    Scanner input = new Scanner(System.in);
    Scanner q;
    String name;
    double balance;

    private Account account = new Account(name, balance);

    public void enterCustomers() {

        System.out.println("Enter customer names or press q to quit entering names: ");

        while (true) {
            String name;
            double balance;
            System.out.print("Enter a customer name: ");
            name = input.next();

            if ("q".equals(name)) { //tried using == to break but wouldnt work so tried .equals since comparing strings and works
                break;
            }

            System.out.print("Enter the opening balance : ");
            balance = input.nextDouble();
            accounts.add(new Account(name, balance));

        }
    }

    public void printBalances() {

        System.out.println("==========================");
        System.out.println("Customer            Balance");
        System.out.println("==========================");
        System.out.println(account.getName() + account.getBalance());

    }

这是存储get方法的类:

class Account {

    private String name;
    private double balance;

    public Account(String name, double balance) {

        this.name = name;
        this.balance = balance;
    }

    public String getName() {

        return name;

    }

    //public void setName(String name) {
    //  this.name = name;
    //}
    public double getBalance() {

        return balance;

    }

    //public void setBalance(double balance) {
    //  this.balance = balance;
    //}
    public void deposit(double amount) {

        balance += amount;

    }

    public void withdrawal(double amount) {

        if (balance >= amount) {
            balance -= amount;
        } else {
            System.out.println(" Insufficient Balance. ");
        }

    }

}

它给我的输出是这样的:

Enter customer names or press q to quit entering names: 
Enter a customer name: john
Enter the opening balance : 200
Enter a customer name: mike
Enter the opening balance : 2
Enter a customer name: q

==========================
Opening account balance
==========================
Customer            Balance
==========================
null0.0

(1)deposit (2)withdraw (0)quit

我不知道为什么出现null 0.0,有人可以解释为什么吗?

2 个答案:

答案 0 :(得分:0)

是否还有对account的引用?

您可能会在某个时候覆盖它,因为它的名称为null,余额为0.0

答案 1 :(得分:0)

现在您正在像这样初始化account

private Account account = new Account(name, balance);

但是这里namebalance未初始化,因此默认情况下分别为null和零。看起来更像是您要打印特定Account对象的名称和余额,因此我将其作为参数传递给printBalances

public void printBalances(Account account) {

    System.out.println("==========================");
    System.out.println("Customer            Balance");
    System.out.println("==========================");
    System.out.println(account.getName() + " : " + account.getBalance());

}

或者如果您想打印accounts ArrayList中的所有余额:

public void printBalances() {
    System.out.println("==========================");
    System.out.println("Customer            Balance");
    System.out.println("==========================");
    for(Account account : accounts) {
        System.out.println(account.getName() + " : " + account.getBalance());
    }
}