教育路上
摘要:使用Scnner类获取用户输入的整数和浮点数,并通过Math数学操作类中的pow(a,b)方法进行求次方。以下是我们为大家整理的,相信大家阅读完后肯定有了自己的选择吧。
2021-06-06 21:09刘永莘
import java.util.Scanner; public class ComputeLoan { public static void main(String[] args) { // Create a Scanner 创建获取用户输入的对象 Scanner input = new Scanner(System.in); // Enter yearly interest rate System.out.print("Enter yearly interest rate, for example 8.25: "); double annualInterestRate = input.nextDouble(); //获取用户输入的浮点数 // Obtain monthly interest rate 对用户输入的浮点数除以1200 double monthlyInterestRate = annualInterestRate / 1200; // Enter number of years System.out.print( "Enter number of years as an integer, for example 5: "); int numberOfYears = input.nextInt();//获取用户输入的整数 // Enter loan amount System.out.print("Enter loan amount, for example 120000.95: "); double loanAmount = input.nextDouble(); //获取用户输入的浮点数 // Calculate payment (使用了Math类中的静态方法pow(a,b)求取啊的次方) double monthlyPayment = loanAmount * monthlyInterestRate / (1 - 1 / Math.pow(1 + monthlyInterestRate, numberOfYears * 12)); double totalPayment = monthlyPayment * numberOfYears * 12; // Display results System.out.println("The monthly payment is " + (int)(monthlyPayment * 100) / 100.0); System.out.println("The total payment is " + (int)(totalPayment * 100) / 100.0); } }
访客的评论 2024/04/19 04:31
文中描述的是准确的吗,如何报名!