我有
System.out.printf("$%.2f\t $%.2f\n ",c.getDifference(),c.getAccValue());
但是c.getAccValue()总是给我错误的输出,我不知道为什么。 getDifference()的公式是getAccValue() - principal,输出正确。
我每季度使用输入5000 8.25 5
CD课程:
package cd;
public class CD {
private int time; //in years, t
private double interest; //interest rate, r
private int principal; //principal, p
private String number;//number of times compounded, n
public CD(int time, double interest, int principal, String number){
this.time = time;
this.interest = interest;
this.principal = principal;
this.number = number;
}
public int getTime(){
return time;
}
public double getInterest(){
return interest;
}
public int getPrincipal(){
return principal;
}
public String getNumber(){
return number;
}
public double getAccValue(){
double A = 0;
interest = interest/100;
if( "daily".equals(number)) {
A = Math.pow(1 + interest/365, 365*time) * principal;
}
else if ("weekly".equals(number)){
A = Math.pow(1 + interest/52, 52*time) * principal;
}
else if ("monthly".equals(number)){
A = Math.pow(1 + interest/12, 12*time) * principal;
}
else if ("quarterly".equals(number)){
A = Math.pow(1 + interest/3, 3*time) * principal;
}
else if ("semiannually".equals(number)){
A = Math.pow(1 + interest/2, 2*time) * principal;
}
else if ("annually".equals(number)){
A = Math.pow(1 + interest/1, 1*time) * principal;
}
return A;
}
public double getDifference(){
return getAccValue() - principal;
}
}
CDTest课程:
package cd;
import java.util.Scanner;
import javax.swing.JOptionPane;
public class CDTest {
/**
* @param args the command line arguments
*/
public static void main(String[] args) {
// TODO code application logic here
String input = JOptionPane.showInputDialog("Enter Principal value, "
+ "Interest rate, time (in years), " + " and number of months"
+ " compunded" + "\n(Separated by spaces)");
Scanner in = new Scanner(input);
int principal = in.nextInt();
double interest = in.nextDouble();
int time = in.nextInt();
String number = in.next();
CD c = new CD(time, interest, principal, number);
System.out.println("Principal " + "Interest " + "Maturity " +
"Number Compounded " + "Amount Gained " +
"Accumulated Value " + "");
System.out.println("========= " + "======== " + "======== "
+ "================= " + "============= " +
"================= ");
System.out.printf(" $" + c.getPrincipal() + "\t ");
System.out.print(c.getInterest() + "%" + "\t ");
System.out.print(c.getTime() + "\t ");
System.out.print(c.getNumber() +" \t");
System.out.printf("$%.2f\t $%.2f\n ",c.getDifference(),c.getAccValue());
}
}
感谢您的帮助
编辑:
当前输出为:
Principal Interest Maturity Number Compounded Amount Gained
========= ======== ======== ================= =============
$5000 8.25% 5 quarterly $2510.99
Accumulated Value
=================
$5020.66
预期输出为:
Principal Interest Maturity Number Compounded Amount Gained
========= ======== ======== ================= =============
$5000 8.25% 5 quarterly $2510.99
Accumulated Value
=================
$7510.99
答案 0 :(得分:0)
.75 * running_total + new_value
您将乘以3.但一年中有四分之三。所以,我会仔细检查这个公式。
另外,你有这一行:
else if ("quarterly".equals(number)){
A = Math.pow(1 + interest/3, 3*time) * principal;
interest = interest/100;
中的。这会永久性地改变利率。但是,getAccValue
会调用getAccValue
。因此,当计算差额时,它将利率除以100 - 然后当自己调用getDifference
时,它再次将利率除以100 。所以现在你的利率微不足道,几乎没有任何钱。