java先息后本计算每期还款金额;实现等额本息代码(80万元的30年期住房贷款,年利率为5.25%,要求算出每一期的还款的本金和利息总额)

  • 【版权所有,文章允许转载,但须以链接方式注明源地址,否则追究法律责任】
  • 【创作不易,点个赞就是对我最大的支持】

前言

仅作为学习笔记,供大家参考
总结的不错的话,记得点赞收藏关注哦!

面试遇到这么一道题:

编程题:银行贷款的还款方式中最常用的是一种叫“等额本息”,还款法,即借款人在约定还款期限内的每一期(月)归还的金额(产生的利息+部分本金)都是相等的,现有一笔总额为80万元的30年期住房贷款,年利率为5.25%,要求算出每一期的还款的本金和利息总额【请写出解决思路和任意一种编程语言实现的主要代码。】

结果:

在这里插入图片描述


话不多少上代码:

import java.util.Scanner;

/**
 * @Author: 程序员温眉
 * @Date: Created in 2020/8/22  16:40
 */
public class demo {
    public static void main(String[] args) {
        System.out.println("请输入贷款本金:");
        Scanner input=new Scanner(System.in);
            double invest = input.nextDouble();     //贷款本金
        System.out.println("请输入年利率:(例如:5.25%就是0.0525)");
            double yearRate = input.nextDouble();     //年利率
            double monthRate = yearRate/12;   //月利率
        System.out.println("请输入借款月数:(例如:30年就是360)");

            int month = input.nextInt();  //还款月数

            System.out.println("本金-->"+invest+"   年利率--->"+yearRate*100+"%"+"  期限--->"+month+"个月");
            System.out.println("--------------------------------------------");

            // 每月本息金额  = (本金×月利率×(1+月利率)^还款月数)÷ ((1+月利率)^还款月数-1))
            double monthIncome = (invest* monthRate * Math.pow(1+monthRate,month))/(Math.pow(1+monthRate,month)-1);
            //四舍五入截取后两位
            String result = String.format("%.2f", monthIncome);

            System.out.println("每月本息金额 : " + result);
            System.out.println("---------------------------------------------------");

            // 每月本金 = 本金×月利率×(1+月利率)^(还款月序号-1)÷((1+月利率)^还款月数-1))
            double monthCapital = 0;
            for(int i=1;i<month+1;i++){
                monthCapital = (invest* monthRate * (Math.pow((1+monthRate),i-1)))/(Math.pow(1+monthRate,month)-1);
                //四舍五入截取后两位
                String monthCapital1 = String.format("%.2f", monthCapital);
                System.out.println("第" + i + "月本金: " + monthCapital1);
            }
            System.out.println("---------------------------------------------------");


            // 每月利息  = 剩余本金 x 贷款月利率
            double monthInterest = 0;
            double capital = invest;
            double tmpCapital = 0;
            double totalInterest = 0;
            for(int i=1;i<month+1;i++){
                capital = capital - tmpCapital;
                monthInterest = capital * monthRate;
                 tmpCapital = (invest* monthRate * (Math.pow((1+monthRate),i-1)))/(Math.pow(1+monthRate,month)-1);
            //四舍五入截取后两位
            String ssa = String.format("%.2f", monthInterest);
            System.out.println("第" + i + "月利息: " + ssa);
                totalInterest = totalInterest + monthInterest;

            }

        System.out.println("-------------------------------------------------");
        //四舍五入截取后两位
        String ss = String.format("%.2f", totalInterest);
        System.out.println("总利息:--->"+ss+" 元"); 
        System.out.println("-------------------------------------------------");
        System.out.println("年利率:--->"+yearRate*100+"%");
        System.out.println("-------------------------------------------------");
        System.out.println("借款期限 : " + month/12+" 年");
        System.out.println("-------------------------------------------------");
        System.out.println("每月还款本息金额 : " + result+" 元");
        }

        /**
         * 获取每月本息金额
         * 计算方式
         * 每月本息金额  = (本金×月利率×(1+月利率)^还款月数)÷ ((1+月利率)^还款月数-1))
         * @param invest  本金
         * @param yearRate 年利率
         * @param month   还款月
         * @return  每月本息金额
         */
        public double getMonthIncome(double invest, double yearRate,int month){
            double monthRate = yearRate/12;   //月利率
            double monthIncome = (invest* monthRate * Math.pow(1+monthRate,month))/(Math.pow(1+monthRate,month)-1);
            return monthIncome;
        }
    }


创作不易,点个赞就是对我最大的支持~


wxgzh:程序员温眉

CSDN:程序员温眉

每天进步一点点的程序员

ThinkPHP是一款开源的PHP框架,用于简化Web应用的开发。对于根据总额度15000元,分成8期,年化利率1.5%等额本息还款情况,我们可以按照以下步骤进行计算: 1.,我们需要计算月利率,因为年利率是1.5%,所以月利率是0.15%(1.5% / 12)。 2. 计算每期本金:由于是等额本息本金会在每期逐渐减少,所以我们需要使用一个递减的方式。总本金除以期数就是每期的初始本金,然后从下一期开始,每期本金都会少于上一期。 3. 每期利息则按照剩余本金计算,因为贷款余额逐期减少,所以利息也逐期降低。 4. 利息计算公式为:每期利息 = 当期本金 * 月利率。 下面是伪代码示例: ```php function calculateMonthlyRepayment($totalAmount, $totalPeriods, $annualInterestRate) { // 将年利率转换为月利率 $monthlyInterestRate = $annualInterestRate / 12; // 总本金 $principal = $totalAmount; // 初始本金 $initialPrincipal = $principal / $totalPeriods; for ($i = 1; $i <= $totalPeriods; $i++) { // 计算每期利息 $interest = $initialPrincipal * $monthlyInterestRate; // 计算当期本金,如果这是第一期,直接等于总本金;否则按照等额本息减少 if ($i > 1) { $previousPrincipal = $initialPrincipal; $initialPrincipal -= $previousPrincipal * ($monthlyInterestRate + 1); } // 每期还款总额 = 本金 + 利息 $repayment = $initialPrincipal + $interest; echo "第$i期,本金: $initialPrincipal元,利息: $interest元,总还款: $repayment元\n"; } } // 调用函数 calculateMonthlyRepayment(15000, 8, 1.5); ```
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值