编写程序计算贷款支付额
2016-04-26 11:29
197 查看
问题描述:程序要求用户输入月利率、还贷年数以及贷款金额,并要求显示月支付金额和总偿还金额。
给定的计算月支付额的公式如下:
月支付额= 贷款总额∗月利率1−1(1+月利率)(年数∗12)
则编写此程序的步骤为:
1) 使用图形用户界面(调用JOptionPane类)提示用户输入年利率、年数和贷款总额;
2) 利用年利率获取月利率;
3) 使用给定的公式计算月支付额;
4) 计算总支付额,总支付额=月支付额12年数;
5) 显示月支付额和总支付额。
以下为完整代码:
import javax.swing.JOptionPane; public class ComputeLoan { public static void main(String[] args) { // TODO Auto-generated method stub //Enter yearly interest rate String yearlyRateString = JOptionPane.showInputDialog( "Enter yearly interest rate,for example 8.25:"); //convert string to double double yearlyRate = Double.parseDouble(yearlyRateString); //obtain monthly interest rate double monthlyRate = yearlyRate /1200; //Enter number of years String Years = JOptionPane.showInputDialog( "Enter number of years as an integer,\nfor example 5:"); //convert string to int int numberOfYears = Integer.parseInt(Years); //Enter loan amount String loanString = JOptionPane.showInputDialog( "Enter loan number,for example 150000.95:"); //convert string to double double loanAmount = Double.parseDouble(loanString); //Calculate payment double monthlyPayment = loanAmount * monthlyRate / (1 - 1 / Math.pow(1 + monthlyRate,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); } }
运行结果如下所示:
相关文章推荐
- dataTable 、dataView、Dataset 区别
- Android开发之activiti节点跳转
- ArrayList数组
- Elasticsearch集群使用ik分词器
- iOS数字媒体开发浅析
- iOS数字媒体开发浅析
- Android屏幕适配经验谈
- CSDN-markdown编辑器语法——字体、字号与颜色
- CodeIgniter third_party 使用demo
- 应用程序的生命周期
- 【剑指offer-Java版】25二叉树中和为某一值的路径
- Android自动检测版本及自动升级
- robotium截图路径设置
- 解决Ubuntu下IDEA无法输入中文问题
- 苹果开源框架ResearchKit简介
- nginx try_files命令
- iOS中多线程原理与runloop介绍
- 欢迎使用CSDN-markdown编辑器
- AndroidSuperDialog
- redis与spring结合使用