java输入贷款年利率,贷款金额,贷款年数 * 输出月还款以及共还款金额

package Second;

import javax.swing.JOptionPane;
/*
 * 输入贷款年利率,贷款金额,贷款年数
 * 输出月还款以及共还款金额
 */
public class Demo2 {

    public static void main(String[] args) {
        // TODO Auto-generated method stub
        //Enter yearly interest rate
                String annualInterestRateString = JOptionPane.showInputDialog(
                        "Enter yearly interest rate,for example 8.25: ");
                
                //Covert string to double
                double annualInterestRate =
                    Double.parseDouble(annualInterestRateString);
                
                //Obtain monthly interest rate
                double monthlyInterestRate = annualInterestRate / 1200;
                
                //Enter number of years
                String numberOfYearString = JOptionPane.showInputDialog(
                        "Enter numbers of years as an integer, \nfor example 5: ");
                
                //Convert string to int
                int numberOfYears = Integer.parseInt(numberOfYearString);
                
                //Enter loan amount
                String loanString = JOptionPane.showInputDialog(
                        "Enter loan amount,for example 120000.95:");
                
                //Convert String to double
                double loanAmount = Double.parseDouble(loanString);
                
                //Calclate 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);
                    
            }

    }




你可能感兴趣的:(java)