本帖最后由 syb012 于 2015-11-24 19:53 编辑
写好了,求大神帮我检查一下
public class Text {
public static void main(String[] args) {
double sub=method(30000,15);
String ss = extracted(sub);
System.out.println("小明在第15个月,应该还款"+ss+"元");
}
//把还款金额转换成需要的格式
private static String extracted(double sub) {
sub=Math.rint(sub*100);
int i=(int) sub;
String string =Integer.toString(i);
String ss=string.replaceAll("(\\d+)(\\d\\d)", "$1\\.$2");
return ss;
}
//算出第n月要还款的金额
private static double method(double sum, int n) {
double m=sum/24;
double h=0;
for(int i=1;i<=n;i++){
if(sum<=0)
return 0;
else if(sum<m)
return sum+0.005*m;
else
h=m+0.005*sum;
sum=sum-m;
}
return h;
}
}
|