您好我无法解决为什么我收到错误无法找到我的简单投资类的符号这是一个编译器错误。
public class Investments
{
//instance variables
private double moneyInvested;
private double investRate;
private int numOfYears;
//construscts a investment with the parameters moneyInvested, double investRate, int numOfYears
public Investments(double moneyInvested, double investRate, int numOfYears)
{
double amount = moneyInvested;
double rate = investRate;
int time = numOfYears;
}
public double ruleOf72()
{
return (72/rate);
}
public int simpleAnnual()
{
return Math.round(amount * Math.pow(1 + rate, time));
}
public int compoundAnnual()
{
return Math.round(amount * Math.pow((1 +rate)^time));
}
}
答案 0 :(得分:1)
此处声明的变量
double amount = moneyInvested;
double rate = investRate;
int time = numOfYears;
都是local variables。它们不能在它们定义的块之外访问,即。在构造函数体外。
您可能打算使用您的实例字段。
this.moneyInvested = moneyInvested;
// ... etc.
您应该重构其余代码以使用这些实例变量。
答案 1 :(得分:0)
试试这种方式
public class Investments {
// instance variables
private double moneyInvested;
private double investRate;
private int numOfYears;
double amount;
double rate;
int time;
public Investments(double moneyInvested, double investRate, int numOfYears) {
this.amount = moneyInvested;
this.rate = investRate;
this.time = numOfYears;
}
public double ruleOf72() {
return (72 / this.rate);
}
public int simpleAnnual() {
return Math.round(this.amount * Math.pow(1 + this.rate, this.time));
}
public int compoundAnnual() {
return Math.round(this.amount * Math.pow((1 + this.rate) ^ this.time));
}
}
答案 2 :(得分:0)
构造函数参数的名称不必作为类中的局部变量存在。我本可以使名字相同,但为了清楚起见,我使用了你的内部名称。
public class Investments
{
//instance variables
private double amount;
private double rate;
private int time;
//constructs an investment with the parameters moneyInvested, investRate, numOfYears
public Investments(double moneyInvested, double investRate, int numOfYears)
{
amount = moneyInvested;
rate = investRate;
time = numOfYears;
}
public double ruleOf72()
{
return (72/rate);
}
public int simpleAnnual()
{
return Math.round(amount * Math.pow(1 + rate, time));
}
public int compoundAnnual()
{
return Math.round(amount * Math.pow((1 +rate)^time));
}
}