首页 新闻 会员 周边

java eclipse

0
[待解决问题]

package yun;

public class Bank {
int savedMoney;
int year;
double interest;
double interestRate=0.29;
public double computerInterest() {
interest=yearinterestRatesavedMoney;
return interest;
}
public void setInterestRate(double rate) {
interestRate=rate;
}

package yun;

public class ConstructionBank extends Bank {
double year;
public double computerInterst() {
super.year=(int)year;
double r=year-(int)year;
int day=(int)(r1000);
double yearInterest=super.computerInterest();
double dayInterest=day
0.0001*savedMoney;
interest=yearInterest+dayInterest;
System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);
return interest;

}

}
package yun;

public class BankOfDalian extends Bank {
double year;
public double computerInterst() {
super.year=(int)year;
double r=year-(int)year;
int day=(int)(r1000);
double yearInterest=super.computerInterest();
double dayInterest=day
0.00012*savedMoney;
interest=yearInterest+dayInterest;
System.out.printf("%d元存在大连银行%d年零%d天的利息:%f元\n",savedMoney,super.year,day,interest);
return interest;
}
}
package yun;

public class SaveMoney {

public static void main(String[] args) {
	// TODO Auto-generated method stub
	int amount=8000;
	ConstructionBank bank1=new ConstructionBank();
	bank1.savedMoney=amount;
	bank1.year=8.236;
	bank1.setInterestRate(0.035);
	double interest1=bank1.computerInterest();
	BankOfDalian bank2=new BankOfDalian();
	bank2.savedMoney=amount;
	bank2.year=8.236;
	bank2.setInterestRate(0.035);
	double interest2=bank2.computerInterest();
	System.out.printf("两个银行利息相差%f元\n",interest2-interest1);
}

}

两个银行利息相差0.000000元

王家兴的主页 王家兴 | 菜鸟二级 | 园豆:210
提问于:2023-11-12 11:33
< >
分享
所有回答(2)
0

你想问什么?

echo_lovely | 园豆:1437 (小虾三级) | 2023-11-13 15:38

运行后,就输出 两个银行利息相差0.000000元
可书上不是怎么写的

支持(0) 反对(0) 王家兴 | 园豆:210 (菜鸟二级) | 2023-11-13 17:55
0

您的代码中有一些拼写错误,导致程序无法正确运行。以下是已经修正的代码:

java

package yun;

public class Bank {
int savedMoney;
int year;
double interest;
double interestRate = 0.29;

public double computeInterest() {
    interest = year * interestRate * savedMoney;
    return interest;
}

public void setInterestRate(double rate) {
    interestRate = rate;
}

}

package yun;

public class ConstructionBank extends Bank {
double year;

public double computeInterest() {
    super.year = (int) year;
    double r = year - (int) year;
    int day = (int) (r * 1000);
    double yearInterest = super.computeInterest();
    double dayInterest = day * 0.0001 * savedMoney;
    interest = yearInterest + dayInterest;
    System.out.printf("%d元存在建设银行%d年零%d天的利息:%f元\n", savedMoney, super.year, day, interest);
    return interest;
}

}

package yun;

public class BankOfDalian extends Bank {
double year;

public double computeInterest() {
    super.year = (int) year;
    double r = year - (int) year;
    int day = (int) (r * 1000);
    double yearInterest = super.computeInterest();
    double dayInterest = day * 0.00012 * savedMoney;
    interest = yearInterest + dayInterest;
    System.out.printf("%d元存在大连银行%d年零%d天的利息:%f元\n", savedMoney, super.year, day, interest);
    return interest;
}

}

package yun;

public class SaveMoney {
public static void main(String[] args) {
int amount = 8000;
ConstructionBank bank1 = new ConstructionBank();
bank1.savedMoney = amount;
bank1.year = 8.236;
bank1.setInterestRate(0.035);
double interest1 = bank1.computeInterest();

    BankOfDalian bank2 = new BankOfDalian();
    bank2.savedMoney = amount;
    bank2.year = 8.236;
    bank2.setInterestRate(0.035);
    double interest2 = bank2.computeInterest();

    System.out.printf("两个银行利息相差%f元\n", interest2 - interest1);
}

}
修正后的代码应该能够正确计算两个银行的利息差异。主要修改包括将 computerInterest 方法改为 computeInterest,并修正了计算利息的公式。请注意在计算年利息时,使用的是 year * interestRate * savedMoney,而不是 year * interestRatesavedMoney。

Technologyforgood | 园豆:5633 (大侠五级) | 2023-11-13 22:19
清除回答草稿
   您需要登录以后才能回答,未注册用户请先注册