- Java code
package day02;import java.io.BufferedReader;import java.io.IOException;import java.io.InputStreamReader;/** * 等额本息还款计算 * @param payment 总贷款数额 200000 * @param ret 年利率 如 5.58 * @param num 分期月份数量 180 * @return 每月还款金额 1642.66 */public class Credit { public static void main(String[] args) throws NumberFormatException, IOException { System.out.println("以下程序将实现等额本息还款的计算"); BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); System.out.println("请输入贷款金额:"); double payment = Double.parseDouble(br.readLine());// 贷款金额 System.out.print("请输入年利率:"); // 年利率 double ret = Double.parseDouble(br.readLine()); System.out.print("请输入贷款期数,即贷款月数:"); // 分期月份数量 int num = Integer.parseInt(br.readLine()); br.close(); System.out.println("每月还款金额为:"+payPerMonth(payment,ret,num)); } public static double payPerMonth(double payment, double ret, int num) { // 等额本息还款计算,其中date 为月利率 double date=ret/12; return (payment*(date*Math.pow(1+date, num)))/Math.pow((1+date),(num-1)); }}
------解决方案--------------------
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 等额本息还款计算
* @param payment 总贷款数额 200000
* @param ret 年利率 如 5.58
* @param num 分期月份数量 180
* @return 每月还款金额 1642.66
*/
public class Credit {
public static void main(String[] args) throws NumberFormatException,
IOException {
System.out.println("以下程序将实现等额本息还款的计算");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入贷款金额:");
double payment = Double.parseDouble(br.readLine());// 贷款金额
System.out.print("请输入年利率:"); // 年利率
double ret = Double.valueOf(br.readLine());
System.out.print("请输入贷款期数,即贷款月数:"); // 分期月份数量
int num = Integer.parseInt(br.readLine());
br.close();
System.out.println("每月还款金额为:"+payPerMonth(payment,ret,num));
}
public static double payPerMonth(double payment, double ret, int num) {
// 等额本息还款计算,其中date 为月利率
double date=ret/12;
return (payment*(date*Math.pow(1+date, num)))/Math.pow((1+date),(num-1));
}
}
这样就ok double ret = Double.parseDouble(br.readLine());改成double ret = Double.valueOf(br.readLine());
------解决方案--------------------
?
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
/**
* 等额本息还款计算
* @param payment 总贷款数额 200000
* @param ret 年利率 如 5.58
* @param num 分期月份数量 180
* @return 每月还款金额 1642.66
*/
public class Credit {
public static void main(String[] args) throws NumberFormatException,
IOException {
System.out.println("以下程序将实现等额本息还款的计算");
BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
System.out.println("请输入贷款金额:");
double payment = Double.parseDouble(br.readLine());// 贷款金额
System.out.print("请输入年利率:"); // 年利率
double ret = Double.valueOf(br.readLine());
System.out.print("请输入贷款期数,即贷款月数:"); // 分期月份数量
int num = Integer.parseInt(br.readLine());