我一遍又一遍地写了这个程序,有人可以告诉我我错过了什么,看起来它是一个小错误,我没有抓到。
这是我想要完成的事情:
设计一个名为Account的类,其中包含:
以下是我目前的情况:
import java.util.Date;
public class Assign22 {
public static void main(String[] args) {
// TODO Auto-generated method stub
Account account1 = new Account(1122, 20000, .045);
account1.withdraw(2500);
account1.deposit(3000);
System.out.println("Date Created:" + java.util.Date dateCreated = new java.util.Date());
System.out.println("Account ID:" + account1.id);
System.out.println("Balance:" + account1.getBalance());
System.out.println("Interest Rate:" + account1.getAnnualInterestRate());
System.out.println("Balance after withdraw of 2500:" + account1.getAnnualInterestRate());
System.out.println("Balance after deposit of 3000:" + account1.getAnnualInterestRate());
System.out.println("Monthly Interest:" + account1.id);
System.out.println("Process completed.");
}
class Account {
//define variables
private int id;
private double balance; // balance for account
private double annualInterestRate; //stores the current interest rate
private Date dateCreated; //stores the date account created
//no arg construtor
Account () {
id = 0;
balance = 0.0;
annualInterestRate = 0.0;
}
//constructor with specific id and initial balance
Account(int newId, double newBalance) {
id = newId;
balance = newBalance;
}
Account(int newId, double newBalance, double newAnnualInterestRate) {
id = newId;
balance = newBalance;
annualInterestRate = newAnnualInterestRate;
}
//accessor/mutator methods for id, balance, and annualInterestRate
public int getId() {
return id;
}
public double getBalance() {
return balance;
}
public double getAnnualInterestRate() {
return annualInterestRate;
}
public void setId(int newId) {
id = newId;
}
public void setBalance(double newBalance) {
balance = newBalance;
}
public void setAnnualInterestRate(double newAnnualInterestRate) {
annualInterestRate = newAnnualInterestRate;
}
//accessor method for dateCreated
public void setDateCreated(Date newDateCreated) {
dateCreated = newDateCreated;
}
//define method getMonthlyInterestRate
double getMonthlyInterestRate() {
return annualInterestRate/12;
}
//define method withdraw
double withdraw(double amount) {
return balance -= amount;
}
//define method deposit
double deposit(double amount) {
return balance += amount;
}
}
}
这是我收到的错误:不确定我错过了什么:
“令牌”Date“上的语法错误,在此令牌dateCreated之后无法解析为Assign22.main(Assign22.java:12)上的变量”
答案 0 :(得分:3)
您不能将变量声明作为String
连接表达式的一部分
System.out.println("Date Created:" + java.util.Date dateCreated = new java.util.Date());
将其拆分
java.util.Date dateCreated = new java.util.Date()
System.out.println("Date Created:" + dateCreated);