import java.util.*;public class BankAccountSimulation { public static void main(String[] args) { Scanner input = new Scanner(System.in); System.out.print("请输入顾客数量:"); int numCustomers = input.nextInt(); // 创建顾客数组 Customer[] customers = new Customer[numCustomers]; for (int i = 0; i < numCustomers; i++) { System.out.printf("请输入顾客%d的汇款次数:", i + 1); int numTransactions = input.nextInt(); System.out.printf("请输入顾客%d每次汇款金额:", i + 1); double amountPerTransaction = input.nextDouble(); customers[i] = new Customer(i + 1, numTransactions, amountPerTransaction); } // 随机安排汇款顺序 List<Customer> customerList = Arrays.asList(customers); Collections.shuffle(customerList); // 模拟汇款 BankAccount bankAccount = new BankAccount(); for (Customer customer : customerList) { for (int i = 0; i < customer.getNumTransactions(); i++) { bankAccount.deposit(customer.getAmountPerTransaction()); } } // 显示总金额 System.out.printf("总金额为:%.2f\n", bankAccount.getBalance()); }}class Customer { private int id; private int numTransactions; private double amountPerTransaction; public Customer(int id, int numTransactions, double amountPerTransaction) { this.id = id; this.numTransactions = numTransactions; this.amountPerTransaction = amountPerTransaction; } public int getId() { return id; } public int getNumTransactions() { return numTransactions; } public double getAmountPerTransaction() { return amountPerTransaction; }}class BankAccount { private double balance; public void deposit(double amount) { balance += amount; } public double getBalance() { return balance; }}在这个代码的基础上显示每次汇款完后的总金额
时间: 2024-03-22 21:37:00 浏览: 53
java代码-import java.util.Scanner;
可以在 BankAccount 类的 deposit 方法里面添加输出语句,表示每次汇款后的总金额。修改后的代码如下:
```
class BankAccount {
private double balance;
public void deposit(double amount) {
balance += amount;
System.out.printf("当前总金额为:%.2f\n", balance);
}
public double getBalance() {
return balance;
}
}
```
这样,在每次汇款后,都会输出当前的总金额。
阅读全文