我试图调用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出现,有人可以解释为什么吗?
呼唤远方
UYOU
相关分类