gpt4 book ai didi

java - 输出未按预期显示

转载 作者:行者123 更新时间:2023-12-02 05:35:47 24 4
gpt4 key购买 nike

经过一段时间的努力,最终克服了一些编译器错误,我终于能够编译并运行我的程序了。然而,我并没有得到任何接近我应该得到的输出。

该程序应该使用一个 Account 类和 2 个子类(SavingsAccount 和 CheckingAccount)以及多种方法来显示某种类型的帐户历史记录。

这是我到目前为止编写的代码:

import java.util.Date;

public class Account {
private int id = 0;
private double balance = 0;
private double annualInterestRate = 0;
private Date dateCreated = new Date();

public Account() {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}

public Account(int id, double balance) {
this.id = id;
this.balance = balance;
}

public int getId() {
return id;
}

public double getBalance() {
return balance;
}

public double getAnnualInterestRate() {
return annualInterestRate;
}

public void setId(int newId) {
this.id = id;
}

public void setBalance(double newBalance) {
this.balance = balance;
}

public void setAnnualInterestRate(double newAnnualInterestRate) {
this.annualInterestRate = annualInterestRate;
this.balance = balance * annualInterestRate;
}

public java.util.Date getDateCreated() {
return dateCreated;
}

public double getMonthlyInterestRate() {
return annualInterestRate/12;
}

public double withdrawal (double withdrawalAmount) {
return balance -= withdrawalAmount;
}

public double deposit (double depositAmount) {
return balance += depositAmount;
}

public static void main(String[] args) {
Account savingsAccount = new Account(1122, 20000);
Account checkingAccount = new Account(1271, 150);
System.out.println("Accounts Created!");
System.out.println(savingsAccount);
System.out.println(checkingAccount);
savingsAccount.setAnnualInterestRate(4.5);
checkingAccount.setAnnualInterestRate(1.25);
System.out.println("Updating Interest");
System.out.println(savingsAccount);
System.out.println(checkingAccount);
savingsAccount.withdrawal(5000);
checkingAccount.withdrawal(300);
System.out.println("Processing Withdrawal");
System.out.println(savingsAccount);
System.out.println(checkingAccount);
savingsAccount.deposit(10000);
checkingAccount.deposit(500);
System.out.println("Processing Deposit");
System.out.println(savingsAccount);
System.out.println(checkingAccount);
System.out.println("Thank you for your business!");
}
}

这是我运行此代码时显示的输出:

Accounts Created!
Account@6aad8bf3
Account@273221e
Updating Interest
Account@6aad8bf3
Account@273221e
Processing Withdrawal
Account@6aad8bf3
Account@273221e
Processing Deposit
Account@6aad8bf3
Account@273221e
Thank you for your business!

但是,输出实际上应该如下所示:

Accounts Created!
Savings Account: ID: 1122 Balance: $20000.00 Rate: 4.5%
Checking Account: ID: 1271 Balance: $150.00 Rate: 1.25%
Updating Interest
Savings Account: ID: 1122 Balance: $20900.00 Rate: 4.5%
Checking Account: ID: 1271 Balance: $151.88 Rate: 1.25%
Processing Withdrawals
Savings Account: ID: 1122 Balance:... Rate: 4.5%
Checking Account: ID: 1271 Balance:... Rate 1.25%
Processing Deposits
Savings Account: ID: 1122 Balance... Rate: 4.5%
Checking Account: ID 1271 Balance... Rate:1.25%
Thank you for your business!

知道我做错了什么吗?

最佳答案

您需要覆盖 toString()自定义类中的方法返回首选输出

关于java - 输出未按预期显示,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24988885/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com