教育路上
摘要:通过使用JOptionPane类中方法来创建弹出提示窗口获取用户的输入并把输入的字符串通过相应的包装类进行拆箱为基本数据类型进行计算结果,最后通过弹出提示窗口显示结果。以下是我们为大家整理的,相信大家阅读完后肯定有了自己的选择吧。
2021-06-06 21:09刘永莘
import javax.swing.JOptionPane; public class ComputeLoanUsingInputDialog { 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); } }
访客的评论 2024/04/25 13:05
文中描述的是准确的吗,如何报名!