gpt4 book ai didi

java - 用 math pow 阅读计算错误

转载 作者:行者123 更新时间:2023-11-29 05:52:15 24 4
gpt4 key购买 nike

我只得到利息金额,而不是我想每月支付的金额,你能告诉我哪里出了问题吗,谢谢。

import java.util.Scanner;

/**
*
* @author
*/
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//variabled decleared
double rate;
double payment;
//input
System.out.print("Enter Loan Amount:");
double principal = input.nextDouble();
System.out.print("Enter Annual Interest:");
double interest = input.nextDouble();
System.out.print("Total payment type:");
String period = input.next();
System.out.print("Enter Loan Length :");
int length = input.nextInt();

//proces
rate = interest / 100;

if (period.equals("monthly")) {
double n = length * 12;
payment = principal * (rate * Math.pow((1 + rate), n) / Math.pow((1 + rate), n));
System.out.printf("Your Monthly Sum is %.2f",payment);
}
}

最佳答案

你的错误在这里:

principal * rate * Math.pow((1 + rate), n) / Math.pow((1 + rate), n)

这等同于只有本金 * 利率。你是说 x = b * a/a。替换为:

 payment = principal * Math.pow((1 + rate), n);

n是年数,不能做n = length/12来得到Monthly。你应该这样做:

payment = (principal * Math.pow((1 + rate), n)) / 12;

关于java - 用 math pow 阅读计算错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13456119/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com