62,634
社区成员




package javajichu;
import javax.swing.JOptionPane;
import java.math.*;
public class Computeloan {
public static void main(String[] args) {
String input1=JOptionPane.showInputDialog("请输入年利率:如8.5");
double 年利率=Double.parseDouble(input1);
double 月利率=年利率/1200;
String input2=JOptionPane.showInputDialog("请输入贷款年数:");
double 年数=Double.parseDouble(input2);
String input3=JOptionPane.showInputDialog("请输入贷款总额:");
double 贷款总额=Double.parseDouble(input3);
double 月支额=贷款总额*月利率/(1-1/Math.pow((1+月利率),年数*12));//计算出月支额
double 总支额=月支额*12*年数;
月支额=(int)(月支额*100/100.0);
总支额=(int)(总支额*100)/100.0;
String output="总支额为:"+总支额+"\n月支额为:"+月支额;//string 定义字符串类型
JOptionPane.showMessageDialog(null, output);
}
}
import javax.swing.JOptionPane;
public class ComputeLoan {
/** Main method */
public static void main(String[] args) {
// Enter yearly interest rate
String annualInterestRateString = JOptionPane.showInputDialog(
"Enter yearly interest rate, for example 8.25:");
// Convert string to double
double annualInterestRate =
Double.parseDouble(annualInterestRateString);
// Obtain monthly interest rate
double monthlyInterestRate = annualInterestRate / 1200;
// Enter number of years
String numberOfYearsString = JOptionPane.showInputDialog(
"Enter number of years as an integer, \nfor example 5:");
// Convert string to int
int numberOfYears = Integer.parseInt(numberOfYearsString);
// Enter loan amount
String loanString = JOptionPane.showInputDialog(
"Enter loan amount, for example 120000.95:");
// Convert string to double
double loanAmount = Double.parseDouble(loanString);
// 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 = "The monthly payment is " + monthlyPayment +
"\nThe total payment is " + totalPayment;
JOptionPane.showMessageDialog(null, output);
}