{利用JAVA算分期还贷}

charlesccc 2009-10-30 08:31:33
要求写一个程序,求出每月的还贷额度..
M=每月的支付额度
P=首付
I=每年的利息
i=每月的利息,I/12
N=还贷的总年限
n=总分期,12*N个月.

M=(P*i*((1+i)^n))/((1+i)^n)-1

本人原程序如下:

/**
*
*
*/
package exam3;

import java.util.*;
public class SmithPayment {
public static void main(String[] args) {
int P,I,N,n,k;
double i,M;
I=0;
N=0;
k=1;

Scanner stdin = new Scanner(System.in);

System.out.println("Please enter the Principal: ");
P = stdin.nextInt();
System.out.println("Please enter the Interest Rates: ");
I = stdin.nextInt();
System.out.println("Please enter the Number of years: ");
N = stdin.nextInt();

i = I/12;
n = 12*N;
M=1;

double temp = 1;
for(k=1; k<n;k++){
temp=temp*(i+1);
}

System.out.println("P: "+P+" I: "+I+" N: "+N+" M: "+M+" i: "+i+" n: "+n+" temp: "+temp+" k: "+k); //之前自己用来检测变量值的,最后要删掉这句.

M=(P*i*temp)/temp-1;
System.out.println("The monthly Payment is : " + M);
}
}


现在程序运行完毕,总是有错误,不对!
要求输入

Please enter the Principal:
100000
Please enter the Interest Rates:
5
Please enter the Number of years:
20

输出结果
The monthly Payment is : $659.96

请大虾帮助!@!
...全文
293 4 打赏 收藏 转发到动态 举报
写回复
用AI写文章
4 条回复
切换为时间正序
请发表友善的回复…
发表回复
charlesccc 2009-10-30
  • 打赏
  • 举报
回复
程序已经搞定了~!~本人发布源代码如下,问题也是和楼上描述的一样,
非常感谢你的解答!@!

/**
*
*
*/
package exam3;

import java.util.*;
public class SmithPayment {
public static void main(String[] args) {
int P,N,n,k;
double i,M,I,InputI;
N=0;
k=1;

Scanner stdin = new Scanner(System.in);

System.out.println("Please enter the Principal: ");
P = stdin.nextInt();
System.out.println("Please enter the Interest Rates: ");
InputI = stdin.nextInt();
I=InputI/100;
i = I/12;
System.out.println("Please enter the Number of years: ");
N = stdin.nextInt();

n = 12*N;
M=1;

double temp = 1;
for(k=1; k<n;k++){
temp=temp*(i+1);
}

M=(P*i*temp)/(temp-1);
System.out.println("The monthly Payment is : " + M);
}
}
wangjn1982 2009-10-30
  • 打赏
  • 举报
回复
i和I,应该是float。

因为定义为double和int得出的结果会取整。
  • 打赏
  • 举报
回复


没想还贷
charlesccc 2009-10-30
  • 打赏
  • 举报
回复
输出结果
The monthly Payment is : $659.96

这个是要求的输出结果,但是上面我的源程序输出的并不是这个结果...
源程序有地方有错误,我想主要是那个求(1+i)^n这出问题了,我不知道这个循环该怎么弄,

希望大侠能帮我纠正.@!@

62,620

社区成员

发帖
与我相关
我的任务
社区描述
Java 2 Standard Edition
社区管理员
  • Java SE
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
暂无公告

试试用AI创作助手写篇文章吧