gpt4 book ai didi

Java.util.Date 空输出

转载 作者:行者123 更新时间:2023-12-01 23:03:09 24 4
gpt4 key购买 nike

我不知道为什么我的代码为 java.util.date 提供了 null。

问题:编写一个测试程序,创建一个Account对象,账户ID为1122,余额为20000,年利率为4.5%。使用提款方法提取 2500 美元,使用存款方法存入 3000 美元,并打印余额、月利息以及该帐户创建的日期
这是我的代码:

import java.util.*;  
public class Account {
private int ID;
private double Balance;
private double annualInterestRate;
private java.util.Date dateCreated;
public Account(){}
public Account(int ID, double Balance, double annualInterestRate){
this.ID=ID;
this.Balance=Balance;
this.annualInterestRate= annualInterestRate;
}
public void setID(int ID){
this.ID=ID;
}
public void setBalance(double Balance){
this.Balance=Balance;
}
public void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate= annualInterestRate;
}
public int getID(){
return ID;
}
public double getBalance(){
return Balance;
}
public double getInterestRate(){
return annualInterestRate;
}
public java.util.Date getDateCreated(){
return dateCreated;
}
public double getMonthlyInterestRate(){
return annualInterestRate/12;
}
public void withDraw(double val){
if ((Balance - val) <0)
{
System.out.println("Offensive content removed from this line");
}
else
{
Balance -= val;
}
}
public void dePosits(double value){
Balance += value;
}

public static void main(String [] arges){
Account account = new Account(1122, 20000,.045);
account.withDraw(2500);
account.dePosits(3000);
System.out.println(account.getBalance());
System.out.println(account.getDateCreated());
}

}

最佳答案

您尚未初始化dateCreated。例如,在构造函数中(或其他地方,具体取决于您的用例)

dateCreated = new java.util.Date();  

或使用所需日期进行初始化的任何其他方式。

关于Java.util.Date 空输出,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23206187/

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