Java编译器返回错误:找不到符号,即使该符号已定义。

huangapple go评论64阅读模式
英文:

Java compiler returning error: cannot find symbol even though the symbol is defined

问题

以下是翻译好的部分:

我是 Java 的新手,仍在努力学习基础知识,我猜我可能犯了一个非常简单的错误。我试图创建一个代码,其中两个 SavingsAccount 类的对象会按月增加利息。这是我编写的代码:

class SavingsAccount {
    
    // 主函数
    public static void main(String args[]) {
        SavingsAccount saver1 = new SavingsAccount(2000.0);
        SavingsAccount saver2 = new SavingsAccount(3000.0);
        SavingsAccount.modifyInterestRate(0.04f);

        System.out.println("S1: " + saver1);
        System.out.println("S2: " + saver2);

        SavingsAccount.modifyInterestRate(0.05f);
        System.out.println("S1: " + saver1);
        System.out.println("S2: " + saver2);
    }
    // 主函数结束
    
    static double annualInterestRate;
    private double savingsBalance;
    
    public SavingsAccount(double balance) {
        savingsBalance = balance;
    }

    public double calculateMonthlyInterest() {
        return (savingsBalance * annualInterestRate) / 12;
    }
    
    public static void modifyInterestRate(double rate) {
        annualInterestRate = rate;
    }
    
    public double getAnnualInterestRate() {
        return annualInterestRate;
    }
    
    public double getSavingsBalance() {
        return savingsBalance;
    }
}

当我尝试运行代码时,出现以下错误:

SavingsAccount.java:29: error: cannot find symbol
                SavingsAccount saver1 = new SavingsAccount(2000.0);
                ^
  symbol:   class SavingsAccount
  location: class SavingsAccount
SavingsAccount.java:29: error: cannot find symbol
                SavingsAccount saver1 = new SavingsAccount(2000.0);
                                            ^
  symbol:   class SavingsAccount
  location: class SavingsAccount
SavingsAccount.java:30: error: cannot find symbol
                SavingsAccount saver2 = new SavingsAccount(3000.0);
                ^
  symbol:   class SavingsAccount
  location: class SavingsAccount
SavingsAccount.java:30: error: cannot find symbol
                SavingsAccount saver2 = new SavingsAccount(3000.0);
                                            ^
  symbol:   class SavingsAccount
  location: class SavingsAccount
SavingsAccount.java:31: error: cannot find symbol
                SavingsAccount.modifyInterestRate(0.04f);
                ^
  symbol:   variable SavingsAccount
  location: class SavingsAccount
SavingsAccount.java:36: error: cannot find symbol
                SavingsAccount.modifyInterestRate(0.05f);
                ^
  symbol:   variable SavingsAccount
  location: class SavingsAccount
6 errors

如果有人能帮助我解决这个问题,我将非常感激。谢谢!

英文:

I am new to java and I am still trying to learn the basics, I'm guessing I've probably made a very simple error. I am trying to create a code where two objects of the class savingsAccount are incremented by a monthly interest. Here is the code that I wrote:

class SavingsAccount {


	//main
	public static void main(String args[]){
		savingsAccount saver1 = new savingsAccount(2000.0);
		savingsAccount saver2 = new savingsAccount(3000.0);
		savingsAccount.modifyAnnualInterestRate(0.04f);

		System.out.println("S1: " + saver1);
		System.out.println("S2: " + saver2);

		savingsAccount.modifyAnnualInterestRate(0.05f);
		System.out.println("S1: " + saver1);
		System.out.println("S2: " + saver2);
	}
	//end main
	
	
	static double annualInterestRate;
	private double savingsBalance;
	
	public void savingsAccount(double balance){
		savingsBalance = balance;
	}

	public double calculateMonthlyInterest(){
		return (savingsBalance * annualInterestRate)/12;
	}
	
	public static void modifyInterestRate(double rate){
		annualInterestRate = rate;
	}
	
	public double getAnnualInterestRate(){
		return annualInterestRate;
	}
	
	public double getSavingsBalance(){
		return savingsBalance;
	}

}

When I attempt to run the code, these errors are returned:

SavingsAccount.java:29: error: cannot find symbol
                savingsAccount saver1 = new savingsAccount(2000.0);
                ^
  symbol:   class savingsAccount
  location: class SavingsAccount
SavingsAccount.java:29: error: cannot find symbol
                savingsAccount saver1 = new savingsAccount(2000.0);
                                            ^
  symbol:   class savingsAccount
  location: class SavingsAccount
SavingsAccount.java:30: error: cannot find symbol
                savingsAccount saver2 = new savingsAccount(3000.0);
                ^
  symbol:   class savingsAccount
  location: class SavingsAccount
SavingsAccount.java:30: error: cannot find symbol
                savingsAccount saver2 = new savingsAccount(3000.0);
                                            ^
  symbol:   class savingsAccount
  location: class SavingsAccount
SavingsAccount.java:31: error: cannot find symbol
                savingsAccount.modifyAnnualInterestRate(0.04f);
                ^
  symbol:   variable savingsAccount
  location: class SavingsAccount
SavingsAccount.java:36: error: cannot find symbol
                savingsAccount.modifyAnnualInterestRate(0.05f);
                ^
  symbol:   variable savingsAccount
  location: class SavingsAccount
6 errors

If anyone could help me with this, I would be very grateful. Thank you!

答案1

得分: 1

你没有为这个类创建构造函数

public class SavingsAccount {

//主函数
public static void main(String args[]) {
    SavingsAccount saver1 = new SavingsAccount(2000.0);
    SavingsAccount saver2 = new SavingsAccount(3000.0);
    saver1.modifyInterestRate(0.04f);
    saver1.calculateMonthlyInterest();
    System.out.println("S1:" + saver1.calculateMonthlyInterest());
    System.out.println("S2:" + saver2.calculateMonthlyInterest());

    saver1.modifyInterestRate(0.05f);
    saver1.calculateMonthlyInterest();
    System.out.println("S1:" + saver1.calculateMonthlyInterest());
    System.out.println("S2:" + saver2.calculateMonthlyInterest());
}
//结束主函数

private double annualInterestRate;
private double savingsBalance;

//构造函数
public SavingsAccount(double balance) {
    savingsBalance = balance;
}

public double calculateMonthlyInterest() {
    return (savingsBalance * annualInterestRate) / 12;
}

public void modifyInterestRate(double rate) {
    annualInterestRate = rate;
}

public double getAnnualInterestRate() {
    return annualInterestRate;
}

}

我认为你正在学习面向对象编程我为你提供了一个更好的示例供你学习

public class Main {

public static void main(String[] args) {

//为SavingsAccount创建两个实例,并将savingsBalance传递给构造函数
SavingsAccount saver1 = new SavingsAccount(2000.0);
SavingsAccount saver2 = new SavingsAccount(3000.0);
//设置年利率的值
saver1.setAnnualInterestRate(0.5);
saver2.setAnnualInterestRate(0.7);

//计算月利息并打印出来
System.out.println("S1:" + saver1.calculateMonthlyInterest());
System.out.println("S2:" + saver2.calculateMonthlyInterest());

//更改利率
saver1.setAnnualInterestRate(0.1);
saver2.setAnnualInterestRate(0.2);
System.out.println("S1:" + saver1.calculateMonthlyInterest());
System.out.println("S2:" + saver2.calculateMonthlyInterest());
}

//为了更好的结构,创建一个独立的类
static class SavingsAccount {

private double savingsBalance = 0;

private double annualInterestRate = 0;

//构造函数
public SavingsAccount(double savingsBalance) {
    this.savingsBalance = savingsBalance;
}

//计算月利息的函数
public double calculateMonthlyInterest() {
    return (savingsBalance * annualInterestRate) / 12f;
}

//类值的getter和setter
public double getAnnualInterestRate() {
    return annualInterestRate;
}

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

public double getSavingsBalance() {
    return savingsBalance;
}

public void setSavingsBalance(double savingsBalance) {
    this.savingsBalance = savingsBalance;
}

}

}
英文:

You didn't create a constructor for the class.

public class SavingsAccount {
//main
public static void main(String args[]){
SavingsAccount saver1 = new SavingsAccount(2000.0);
SavingsAccount saver2 = new SavingsAccount(3000.0);
saver1.modifyInterestRate(0.04f);
saver1.calculateMonthlyInterest();
System.out.println("S1: " + saver1.calculateMonthlyInterest());
System.out.println("S2: " + saver2.calculateMonthlyInterest());
saver1.modifyInterestRate(0.05f);
saver1.calculateMonthlyInterest();
System.out.println("S1: " + saver1.calculateMonthlyInterest());
System.out.println("S2: " + saver2.calculateMonthlyInterest());
}
//end main
private double annualInterestRate;
private double savingsBalance;
//Constructor
public SavingsAccount(double balance){
savingsBalance = balance;
}
public double calculateMonthlyInterest(){
return (savingsBalance * annualInterestRate)/12;
}
public void modifyInterestRate(double rate){
annualInterestRate = rate;
}
public double getAnnualInterestRate(){
return annualInterestRate;
}
}

I think you are learning oop. I placed a better example for your study.

public class Main {
public static void main(String[] args) {
//Create two instances for SavingsAccount and pass the savingsBalance to constructor
SavingsAccount saver1 = new SavingsAccount(2000.0);
SavingsAccount saver2 = new SavingsAccount(3000.0);
//Set the value rate of the AnnualInterestRate
saver1.setAnnualInterestRate(0.5);
saver2.setAnnualInterestRate(0.7);
//calculate the Monthly Interest and print it out
System.out.println("S1: " + saver1.calculateMonthlyInterest());
System.out.println("S2: " + saver2.calculateMonthlyInterest());
//Change the rate
saver1.setAnnualInterestRate(0.1);
saver2.setAnnualInterestRate(0.2);
System.out.println("S1: " + saver1.calculateMonthlyInterest());
System.out.println("S2: " + saver2.calculateMonthlyInterest());
}
//Create a separated class for a better structure
static class SavingsAccount {
private double savingsBalance = 0;
private double annualInterestRate = 0;
//Constructor
public SavingsAccount(double savingsBalance){
this.savingsBalance = savingsBalance;
}
//Function for calculat the eMonthly Interest
public double calculateMonthlyInterest(){
return (savingsBalance * annualInterestRate)/12f;
}
//Getter setter for the class value
public double getAnnualInterestRate(){
return annualInterestRate;
}
public void setAnnualInterestRate(double annualInterestRate){
this.annualInterestRate = annualInterestRate;
}
public double getSavingsBalance(){
return savingsBalance ;
}
public void setSavingsBalance(double savingsBalance){
this.savingsBalance = savingsBalance;
}
}
}

答案2

得分: 0

class SavingsAccount {

    // 主方法
    public static void main(String args[]){
        SavingsAccount saver1 = new SavingsAccount();
        SavingsAccount saver2 = new SavingsAccount();

        saver1.savingsAccount(2000.0);
        saver2.savingsAccount(3000.0);

        SavingsAccount.modifyInterestRate(0.04);

        System.out.println("S1: " + saver1.calculateMonthlyInterest());
        System.out.println("S2: " + saver2.calculateMonthlyInterest());

        SavingsAccount.modifyInterestRate(0.05);
        System.out.println("S1: " + saver1.calculateMonthlyInterest());
        System.out.println("S2: " + saver2.calculateMonthlyInterest());
    }
    // 结束主方法

    static double annualInterestRate;
    private double savingsBalance;

    public void savingsAccount(double balance){
        savingsBalance = balance;
    }

    public double calculateMonthlyInterest(){
        return (savingsBalance * annualInterestRate)/12;
    }

    public static void modifyInterestRate(double rate){
        annualInterestRate = rate;
    }

    public double getAnnualInterestRate(){
        return annualInterestRate;
    }

    public double getSavingsBalance(){
        return savingsBalance;
    }
}
英文:

I don't know what you are trying to do but here is your compilable code

class SavingsAccount {
//main
public static void main(String args[]){
SavingsAccount saver1 = new SavingsAccount();
SavingsAccount saver2 = new SavingsAccount();
saver1.savingsAccount(2000.0);
saver2.savingsAccount(3000.0);
SavingsAccount.modifyInterestRate(0.04);
System.out.println("S1: " + saver1.calculateMonthlyInterest());
System.out.println("S2: " + saver2.calculateMonthlyInterest());
SavingsAccount.modifyInterestRate(0.05);
System.out.println("S1: " + saver1.calculateMonthlyInterest());
System.out.println("S2: " + saver2.calculateMonthlyInterest());
}
//end main
static double annualInterestRate;
private double savingsBalance;
public void savingsAccount(double balance){
savingsBalance = balance;
}
public double calculateMonthlyInterest(){
return (savingsBalance * annualInterestRate)/12;
}
public static void modifyInterestRate(double rate){
annualInterestRate = rate;
}
public double getAnnualInterestRate(){
return annualInterestRate;
}
public double getSavingsBalance(){
return savingsBalance;
}

}

答案3

得分: 0

你的类名是SavingsAccount,但是当你创建对象实例时,你写成了savingsAccount;

Java是区分大小写的语言,所以你需要将savingsAccount更改为SavingsAccount。

并且你缺少了一个构造函数:

public SavingsAccount(double savingsBalance){
      this.savingsBalance = savingsBalance;
}
英文:

Your class name is SavingsAccount, but when you create an object instance, you write savingsAccount;

Java is a case-sensitive language, so you need to change savingsAccount to SavingsAccount

And you are missing a constructor:

public SavingsAccount(double savingsBalance){
      this. savingsBalance = savingsBalance;
}

答案4

得分: -1

Java是一种区分大小写的语言。创建对象时,您需要使用与类名相同的名称。在您的情况下,您必须使用

SavingsAccount saver1 = new SavingsAccount(2000.0);
英文:

Java is a Case-sensitive language. When creating objects you need to use the same name as the class name. In your case you have to use

SavingsAccount saver1 = new SavingsAccount(2000.0);

huangapple
  • 本文由 发表于 2020年10月17日 02:26:58
  • 转载请务必保留本文链接:https://go.coder-hub.com/64394534.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定