如何修复"不兼容的类型"我的程序错误?

时间:2015-05-10 02:38:48

标签: java compiler-errors incompatibletypeerror

我正在尝试为Java创建一个帐户程序。我的大部分内容都输出了,但我在日期和打印方面遇到了麻烦。

我正在尝试

  • 按月利息打印帐户。
  • 打印帐户月利率。
  • 从帐户中提取7,500美元。
  • 再次按月打印帐户。
  • 在账户中存入11,0000美元。
  • 打印每月兴趣

错误

  • DateCreated的课堂上。

    Incompatible type: Date cannot be converted to String
    
  • 在我的主要课程中,getDateCreatedgetAnnualInterest都找不到符号错误。

这是我的主要代码

    package testaccount;

    public class TestAccount {

    public static void main(String[] args) {
        Account account1 = new Account(5648, 27000, 3.9);
        account1.withdraw(7500);
        account1.deposit(11000); 
        System.out.println("Your current balance is" +account1.getBalance());
        System.out.println("Monthly interest is"+ account1.getAnnualInterestRate);
        System.out.println("The account was created on" + account1.getDateCreated);
    }

}

这是我班级的代码

    package testaccount;

    import java.util.Date;

    public class Account {

    private int id;
    private double balance;
    private double annualInterestRate;
    private Date dateCreated;

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

    Account(int Nid, double NBalance) {
        id = Nid;
        balance = NBalance;
    }

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

    }

    public int getId() {
        return id;
    }

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

    public double getBalance() {
        return balance;
    }

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

    public double getAnnualInterestRate() {
        return annualInterestRate;
    }

    double withdraw(double amount) {
        return balance -= amount;
    }

    double deposit(double amount) {
        return balance += amount;
    }

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

    public void setDateCreated(Date newDateCreated){
        this.dateCreated = dateCreated;
    }

    double getannaulnterestRate(){
        return annualInterestRate/12;
    }

    public String getDateCreated() {
        return dateCreated;
    }

}

3 个答案:

答案 0 :(得分:0)

您需要以String格式返回日期:

public String getDateCreated() {
        return dateCreated.toString();
    }

在实例化帐户对象时,您似乎也没有设置日期。您需要使用它:

account.setDateCreated(new Date());

或者更好地在构造函数本身中传递Date。看来你需要阅读关于类的构造函数,方法和其他基本结构。

答案 1 :(得分:0)

你得到的原因是找不到符号错误的原因是你试图将它们作为字段访问它们实际上是方法:

System.out.println("Monthly interest is"+ account1.getAnnualInterestRate);
System.out.println("The account was created on" + account1.getDateCreated);

应该是:

System.out.println("Monthly interest is"+ account1.getAnnualInterestRate());
System.out.println("The account was created on" + account1.getDateCreated());

答案 2 :(得分:0)

主类代码

public class TestProgram {

    public static void main(String[] args) throws FileNotFoundException {

        Date date= new Date();
         Account account1 = new Account(5648, 27000, date);
            account1.withdraw(7500);
            account1.deposit(11000); 
            System.out.println("Your current balance is" +account1.getBalance());
            System.out.println("Monthly interest is"+ account1.getAnnualInterestRate());
            System.out.println("The account was created on" + account1.getDateCreated());
            }
    }

帐户类代码

import java.util.Date;


public class Account {

     private int id;
        private double balance;
        private double annualInterestRate;
        private Date dateCreated;

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

        Account(int Nid, double NBalance) {
            id = Nid;
            balance = NBalance;
        }


        Account(double balance, double annualInterestRate, Date dateCreated ) {
            this.balance = balance;
            this.annualInterestRate = annualInterestRate;
            this.dateCreated=dateCreated;

        }

        Account(int id, double balance, double annualInterestRate, Date dateCreated ) {
            this.id = id;
            this.balance = balance;
            this.annualInterestRate = annualInterestRate;
            this.dateCreated=dateCreated;

        }

        public int getId() {
            return id;
        }

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

        public double getBalance() {
            return balance;
        }

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

        public double getAnnualInterestRate() {
            return annualInterestRate;
        }

        double withdraw(double amount) {
            return balance -= amount;
        }

        double deposit(double amount) {
            return balance += amount;
        }

        public void setAnnualInterestRate(double annualInterestRate) {
            this.annualInterestRate = annualInterestRate;
        }
        public void setDateCreated(Date newDateCreated){
        this.dateCreated = dateCreated;
    }
        double getannaulnterestRate(){
        return annualInterestRate/12;
    }
        public Date getDateCreated() {
            return dateCreated;
        }


}