猿问

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

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


这是调用它的类:


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;

            }


    }

这是存储 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出现,有人可以解释为什么吗?


陪伴而非守候
浏览 123回答 2
2回答

呼唤远方

有没有更多的参考account?您可能会在某个时候覆盖它,因为它的名称null为0.0

UYOU

现在你正在account像这样初始化:private Account account = new Account(name, balance);但是这里name和balance没有初始化,所以null默认情况下它们分别是和 零。看起来更像是您想打印特定Account对象的名称和余额,因此我会将其作为参数传递给printBalances:public void printBalances(Account account) {&nbsp; &nbsp; System.out.println("==========================");&nbsp; &nbsp; System.out.println("Customer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Balance");&nbsp; &nbsp; System.out.println("==========================");&nbsp; &nbsp; System.out.println(account.getName() + " : " + account.getBalance());}或者,如果您想打印accounts ArrayList:public void printBalances() {&nbsp; &nbsp; System.out.println("==========================");&nbsp; &nbsp; System.out.println("Customer&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; Balance");&nbsp; &nbsp; System.out.println("==========================");&nbsp; &nbsp; for(Account account : accounts) {&nbsp; &nbsp; &nbsp; &nbsp; System.out.println(account.getName() + " : " + account.getBalance());&nbsp; &nbsp; }}
随时随地看视频慕课网APP

相关分类

Java
我要回答