这是我必须回答的问题: (比较不同利率的贷款)编写一个程序,让用户输入贷款金额和贷款期限,并显示每个利率的月付款和总付款从5%到8%,增量为1 / 8。
import java.util.Scanner; // Scanner is in java.util
public class FinancialApp {
public static void main(String[] args) {
Scanner input = new Scanner(System.in); // Scanner object
System.out.print("Enter loan amount: "); // input value for loan
double loanAmount = input.nextDouble();
System.out.print("Enter number of years as an integer: ");
int years = input.nextInt();
System.out.println("Intrest Rate" + "\t" + "Monthly Payment" + "\t" + "Total Payment");
for(double i = 5.000; i <= 8.000; i += 0.125){
double monthlyIntrest = i / 1200;
double monthlyPay = loanAmount * monthlyIntrest / (1 - 1 / Math.pow(1 + monthlyIntrest, years * 12));
double totalPay = monthlyPay * years * 12;
System.out.println(i + "%" + "\t" + monthlyPay + "\t" + totalPay);
}
}
}
答案 0 :(得分:1)
首先,我请你不要使用Double
来处理金钱。
您可以尝试在,
之后只有2位数字System.out.printf("%.2f", yourNumber);