Java---计算机贷款支付额计算(用对话框实现)
2015-12-19 00:03
344 查看
本例演示如何编写程序来计算贷款支付问题。
下面是编写程序的步骤:
1.提示用户输入年利率、年数和贷款总额
2.利用年利率算出月利率
3.通过前面的公式计算月支付额。
4.计算总支付额,它是月支付额乘以12再乘以年数。
5.在消息对话框中显示月支付额和总支付额。
注意:如果在输入对话框中点击取消,无字符串返回,系统会出现运行时错误。
下面是编写程序的步骤:
1.提示用户输入年利率、年数和贷款总额
2.利用年利率算出月利率
3.通过前面的公式计算月支付额。
4.计算总支付额,它是月支付额乘以12再乘以年数。
5.在消息对话框中显示月支付额和总支付额。
package cn.hncu.Chapter2; import javax.swing.JOptionPane; /** * @author hncu_chx * * Mylove amin * */ public class ComputeLoan { 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 tow 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---计算机贷款支付额计算(用对话框实现)
- 数据结构 Stark
- C++的不同数据类型值的比较语句 http://www.nowcoder.com/profile/380772/test/2198830/25642?onlyWrong=0
- 【Java基础】Java网络编程基础知识
- 菜鸟教程 http://www.runoob.com/
- 数据结构 查找及排序算法、直接插入排序及顺序查找(c语言实现)
- 网络基础知识-3
- 网络请求的封装历程
- 数据结构 二叉树的递归算法、前序、中序、后序遍历(c语言实现)
- Apache HttpClient4.5(一)
- <LeetCode OJ> 13 / 12 Roman to Integer & Integer to Roman
- win10 计算机内存不足 请保存文件关闭这些程序
- 数据结构(一)循环链表 约瑟夫环
- 服务器socket与安卓端socket对接
- 数据结构期末总结(对反转课堂的个人体会)
- <LeetCode OJ> 7. Reverse Integer
- http://thecodeplayer.com/
- <网络编程培训之七> 实现Traceroute
- PHP 版HTTP GET,POST,上传文件
- Unix网络编程基本函数