计算贷款支付额
月支付额 = 贷款总额月利率 /(1-1/(1+月利率)^(年数12))
import java.util.Scanner;
class Main {
public static void main(String[] args) {
//Create a Scanner
Scanner input = new Scanner(System.in);
//Enter yearly interest rate
System.out.print("请输入年利率,比如 4.5:");
double annualInterestRate = input.nextDouble();
//Obtain monthly interest rate
double monthlyInterestRate = annualInterestRate / 1200;
//Enter number of years
System.out.print("请输入年数,比如 5:");
int numberOfYears = input.nextInt();
//Enter loan amount
System.out.print("请输入贷款总额,比如 300000:");
double loanAmount = input.nextDouble();
//Calculate payment
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1/Math.pow(1 + monthlyInterestRate,numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
//Display results
System.out.println("每月还款:" + (int)(monthlyPayment * 100) / 100.0);
System.out.println("还款总额:" + (int)(totalPayment * 100) / 100.0);
input.close();
}
}
字符串转换为数字
int intValue = Integer.parseInt(intString);
double doubleValue = Double.parseDouble(doubleString);
使用输入对话框计算贷款支付额
import javax.swing.JOptionPane;
class Main {
public static void main(String[] args) {
//Enter yearly interest rate
String annualInterestRateString = JOptionPane.showInputDialog("输入贷款年利率,比如4.85:");
//Convert string to double
double annualInterestRate = Double.parseDouble(annualInterestRateString);
//Obtain monthly interest rate
double monthlyInterestRate = annualInterestRate / 1200;
//Enter numbe of years
String numberOfYearsString = JOptionPane.showInputDialog("输入还款年数,比如30:");
//Convert string to int
int numberOfYears = Integer.parseInt(numberOfYearsString);
//Enter loan amout
String loadString = JOptionPane.showInputDialog("输入贷款总额,比如300000:");
//Convert string to double
double loanAmount = Double.parseDouble(loadString);
//Calculate payment
double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12));
double totalPayment = monthlyPayment * numberOfYears * 12;
//Format to keep two digits after the decimal point
monthlyPayment = (int)(monthlyPayment * 100) / 100.0;
totalPayment = (int)(totalPayment * 100) / 100.0;
//Display results
String output = "每月还款为:" + monthlyPayment + "\n总还款额为:" + totalPayment;
JOptionPane.showMessageDialog(null,output);
}
}
计算未来投资值
import javax.swing.JOptionPane;
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//
System.out.println("Enter investment amount:");
double investmentAmount = input.nextDouble();
//
System.out.println("Enter annually interest rate:");
double annuallyInteresRate = input.nextDouble();
double monthlyInterestRate = annuallyInteresRate / 1200;
//
System.out.println("Enter numer of years:");
int numberOfYears = input.nextInt();
double futureInvestmentValue = investmentAmount *
Math.pow((1 + monthlyInterestRate), numberOfYears * 12);
futureInvestmentValue = (int)(futureInvestmentValue * 100) / 100.0;
System.out.println("Accumuulated value is " +
futureInvestmentValue);
}
}
计算复利
import java.util.*;
class Main {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println("请输入每月存款额:");
double monthlySaveAmount = input.nextDouble();
System.out.println("请输入年利率:");
double annuallyInterestRate = input.nextDouble();
double monthlyInterestRate = annuallyInterestRate / 1200;
System.out.println("请输入存入月份:");
int numbersOfMonth = input.nextInt();
double amoutOfMoney = 0;
for(int i = 0; i < numbersOfMonth; i++){
amoutOfMoney = amoutOfMoney + monthlySaveAmount;
amoutOfMoney = amoutOfMoney * (1 + monthlyInterestRate);
amoutOfMoney = (int)(amoutOfMoney * 1000) / 1000.0;
System.out.println("第" + i + "个月的帐面值为:" + amoutOfMoney);
}
input.close();
}
}