0
这里是我必须回答的问题: (比较各种利率的贷款)编写一个程序,让用户输入贷款数量和贷款期限以年数显示每个月的利息支付总额从5%到8%不等,增加1/8。小数点后的位数和固定列格式化
import java.util.Scanner; // Scanner is in java.util
public class FinancialApp {
\t public static void main(String[] args) {
\t Scanner input = new Scanner(System.in); // Scanner object
\t System.out.print("Enter loan amount: "); // input value for loan
\t \t double loanAmount = input.nextDouble();
\t System.out.print("Enter number of years as an integer: ");
\t \t int years = input.nextInt();
\t System.out.println("Intrest Rate" + "\t" + "Monthly Payment" + "\t" + "Total Payment");
\t \t for(double i = 5.000; i <= 8.000; i += 0.125){
\t \t \t double monthlyIntrest = i/1200;
\t \t \t double monthlyPay = loanAmount * monthlyIntrest/(1 - 1/Math.pow(1 + monthlyIntrest, years * 12));
\t \t \t double totalPay = monthlyPay * years * 12;
\t \t \t System.out.println(i + "%" + "\t" + monthlyPay + "\t" + totalPay);
\t \t }
\t }
}
要简单地回答你的问题,它可以像'System.out.printf(“%2F \ n”,12345.456)来来达到的; '但是你真的不应该为了钱而使用'双'。看看BigDecimal –
将你的double转换为BigDecimal并使用setScale方法。 –