如何在程序中修复“不兼容类型”错误?

时间:2022-11-07 19:26:00

I am trying to make a make a account program for Java. I have most of it type out but I am having trouble with the date and printing.

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

I am trying to

我在尝试着

  • Print the accounts monthly interest.
  • 打印帐户每月利息。

  • Print the accounts monthly interest rate.
  • 打印帐户月利率。

  • Withdraw $7,500 from the account.
  • 从账户中提取7,500美元。

  • Print the accounts monthly interest again.
  • 再次打印帐户每月利息。

  • Deposit $11,0000 in the account.
  • 在账户中存入11,0000美元。

  • Print the monthly Interest
  • 打印每月利息

ERRORS

  • In my class in DateCreated.

    在我的DateCreated课程中。

    Incompatible type: Date cannot be converted to String
    
  • In my main class, getDateCreated and getAnnualInterest both have cannot find symbol errors.
  • 在我的主类中,getDateCreated和getAnnualInterest都找不到符号错误。

Here is my code for main

这是我的主要代码

    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);
    }

}

Here's the code for my class

这是我班级的代码

    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 个解决方案

#1


You need to return date in String format:

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

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

It seems you are also not setting date when instantiating an account object. You need to use it like:

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

account.setDateCreated(new Date());

Or better pass Date in constructor itself. It seems you need to read about constructor, method and other basic constructs of a class.

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

#2


The reason you are getting cannot find symbol errors is that you are trying to access them as fields when they are in fact methods:

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

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

Should be:

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

#3


Main Class Code

主类代码

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());
            }
    }

Account class Code

帐户类代码

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;
        }


}

#1


You need to return date in String format:

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

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

It seems you are also not setting date when instantiating an account object. You need to use it like:

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

account.setDateCreated(new Date());

Or better pass Date in constructor itself. It seems you need to read about constructor, method and other basic constructs of a class.

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

#2


The reason you are getting cannot find symbol errors is that you are trying to access them as fields when they are in fact methods:

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

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

Should be:

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

#3


Main Class Code

主类代码

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());
            }
    }

Account class Code

帐户类代码

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;
        }


}