gpt4 book ai didi

java - 我没有让循环在每次循环后获取新值?

转载 作者:行者123 更新时间:2023-12-02 07:25:52 25 4
gpt4 key购买 nike

我正在尝试解决一个问题,即我尝试让循环在每次循环后采用新值。该程序应该对我的利息支付、本金支付和当前余额进行罚款,这样做,每次我循环时,我的利息应该会越来越少,但它只会循环第一个答案超过

这是输出,第一个答案是正确的,但其他三个答案是正确的

    Enter Loan Amount:500
Enter Annual Interest:50
Total payment:4
Enter Loan Length :1
Interest PaymentPrincipal PaymentCurrent Balance
Interest Payment Principal Payment Current Balance 62.5
62.5
62.5
62.5

应该是第一次付款后余额的 12.5%。

    public static void main(String[] args) {
Scanner input = new Scanner(System.in);
//variabled decleared
double rate;
double payment;
int amt = 1;
//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:");//12=monthly,4= quartely,2=semi-annually and 1=annually
double period = input.nextDouble();
System.out.print("Enter Loan Length :");
int length = input.nextInt();

//proces
rate = interest / 100;
double period_rate = rate / period;
double n = period * length;
payment = (principal*Math.pow((1+period_rate),n))/n;





System.out.printf("\n"+"Interest Payment"+" Principal Payment "+" Current Balance ");

for(int i=1; i<=n; i++){
double principal_payment=0;
double current_balance;
double payment_interest;

current_balance=(principal-principal_payment);
payment_interest=(principal*period_rate);
principal_payment=(payment-payment_interest);
principal=current_balance;



System.out.println(payment_interest+"");

}

最佳答案

忘掉编程吧。拿起铅笔和纸,花几个月的时间进行计算。然后将这些计算转移到代码中。当你在了解问题所在之前就开始编码,你就有点超前了。

关于java - 我没有让循环在每次循环后获取新值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13590364/

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