Java方法中的抽象方法工作不正常
在以下问题上,我无法显示我的复利和贷款。我必须有一个抽象的超类和两个方法,一个集合和一个获取存储原理量的方法,一个抽象集合和一个获取存储速率和年数的方法。当我运行这个项目时,我不知道我做错了什么,不管我做什么,复合物和贷款都保持在0.0。请帮忙!!谢谢哦,我还必须创建对象的引用,我知道怎么做。注:我最初有公共双年费和公共双年费,但由于它不起作用,我创建了第2年费率2等Java方法中的抽象方法工作不正常,java,abstract-class,abstract,Java,Abstract Class,Abstract,在以下问题上,我无法显示我的复利和贷款。我必须有一个抽象的超类和两个方法,一个集合和一个获取存储原理量的方法,一个抽象集合和一个获取存储速率和年数的方法。当我运行这个项目时,我不知道我做错了什么,不管我做什么,复合物和贷款都保持在0.0。请帮忙!!谢谢哦,我还必须创建对象的引用,我知道怎么做。注:我最初有公共双年费和公共双年费,但由于它不起作用,我创建了第2年费率2等 public static void main(String[] args) { //Instance of S
public static void main(String[] args) {
//Instance of Scanner
Scanner input = new Scanner(System.in);
//Instances
Guarantee guara = new Guarantee();
CompoundInterest compI = new CompoundInterest();
Loan borrow = new Loan();
System.out.println("Please enter the principle amount: ");
double principleAmount = input.nextDouble();
System.out.println("Please enter the rate ");
double rate = input.nextDouble();
System.out.println("Please enter the years : ");
double years = input.nextDouble();
Funds ref = guara;
ref.setPrincipleAmount(principleAmount);
ref.setData(rate, years);
System.out.printf("With a principle amount of $%.2f , an interest rate of %.2f, and %.2f years, you would have earned $%.2f\n", principleAmount, rate, years, guara.getData());
ref = compI;
ref.setData(rate, years);
System.out.printf("With a principle amount of $%.2f, an interest rate of %.2f, and %.2f, you would have a compound interest of $%.2f\n", principleAmount, rate, years, ref.getData());
//Instances
}//End main
}//End class
//Super class
abstract class Funds{
//Instance variables
public double principleAmount;
public double rate;
public double years;
public double rate2;
public double rate3;
public double year2;
public double year3;
public void setPrincipleAmount(double principleAmount){
this.principleAmount = principleAmount;
}//End setPrincipleAmount
public double getPrincipleAmount(){
return principleAmount;
}//End getPrincipleAmount
public abstract void setData(double rate, double years);
public abstract double getData();
}//End abstract class Funds
class Guarantee extends Funds{
@Override
public void setData(double rate, double years) {
this.rate = rate;
this.years = years;
}
@Override
public double getData() {
return (principleAmount * rate * years);
}
}//End sub class Guarantee
class CompoundInterest extends Funds{
@Override
public void setData(double rate, double years) {
// TODO Auto-generated method stub
this.rate = rate2;
this.years = year2;
}
@Override
public double getData() {
// TODO Auto-generated method stub
return (principleAmount * Math.pow(1 + rate2, year2));
}
}//End sub class CompundInterest
class Loan extends Funds{
@Override
public void setData(double rate, double years) {
// TODO Auto-generated method stub
this.rate = rate3;
this.years = year3;
}
@Override
public double getData() {
// TODO Auto-generated method stub
return (principleAmount * rate3 * year3) + principleAmount;
}
应该是
@Override
public void setData(double rate, double years) {
// TODO Auto-generated method stub
this.rate2 = rate;
this.years2 = year;
}
请重新检查你的作业。它们似乎不正确。看起来您还没有弄清楚作业。在x=y中;,变量x获取变量y的值。所以你所有的作业都是错误的。这个比率=比率3;应为。比率3=比率;,等等,嗨!谢谢你回答我的问题,不过,我会把图像倒转过来。Y=X及其真值,因为X=Y Y必须等于X。我曾经这样尝试过,但它仍然保持在0.0x=y,这不是比较,而是赋值。颠倒它是不明智的。是的,只是确认变量的赋值不会影响任何事情。谢谢你的回答!这段代码中还有很多其他的bug,但这是一开始会给您带来最多问题的bug。首先尝试一个简单得多的问题;对于第一次尝试,您咬得太多了。可以,但我最初的代码让所有类都这样做:this.rate=rate;这个年=年。我知道在上面,它一定是这个。rate2=rate;这个。比率3=比率;对的还是我遗漏了什么?但仍然有所有这些变量,我已经翻转,放回,销毁,重新创建,制造了更多,但我的化合物和贷款保持在0.0
@Override
public void setData(double rate, double years) {
// TODO Auto-generated method stub
this.rate2 = rate;
this.years2 = year;
}