要求写一个程序,求出每月的还贷额度..
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请大虾帮助!@!

解决方案 »

  1.   

    输出结果 
    The monthly Payment is : $659.96 这个是要求的输出结果,但是上面我的源程序输出的并不是这个结果...
    源程序有地方有错误,我想主要是那个求(1+i)^n这出问题了,我不知道这个循环该怎么弄,希望大侠能帮我纠正.@!@
      

  2.   

    i和I,应该是float。因为定义为double和int得出的结果会取整。
      

  3.   

    程序已经搞定了~!~本人发布源代码如下,问题也是和楼上描述的一样,
    非常感谢你的解答!@!/**
     * 
     * 
     */
    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);
    }
    }