Java I';我不会让循环在每次循环后都取一个新值吗?

Java I';我不会让循环在每次循环后都取一个新值吗?,java,Java,我试图解决一个问题,我试图让我的循环在每次循环后取一个新值。这个程序应该是罚款我的利息支付,本金支付和当前余额,这样做我的利息应该是少一点,每次我循环,但它只是循环第一个答案超过一个周期 Enter Loan Amount:500 Enter Annual Interest:50 Total payment:4 Enter Loan Length :1 Interest PaymentPrincipal PaymentCurrent Balance

我试图解决一个问题,我试图让我的循环在每次循环后取一个新值。这个程序应该是罚款我的利息支付,本金支付和当前余额,这样做我的利息应该是少一点,每次我循环,但它只是循环第一个答案超过一个周期

    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
这是第一个答案正确,但其他三个答案不正确的输出

    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%

    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
    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+"");

         }
publicstaticvoidmain(字符串[]args){
扫描仪输入=新扫描仪(System.in);
//可变的减少
双倍费率;
双倍支付;
国际金额=1;
//输入
系统输出打印(“输入贷款金额:”);
double principal=input.nextDouble();
系统输出打印(“输入年度利息:”);
双兴趣=输入。nextDouble();
System.out.print(“总付款:”;//12=每月,4=四分之一,2=半年,1=每年
双周期=input.nextDouble();
System.out.print(“输入贷款长度:”);
int length=input.nextInt();
//过程
利率=利息/100;
双周期利率=利率/周期;
双n=周期*长度;
付款=(本金*数学功率((1+期间费率),n))/n;
System.out.printf(“\n”+“利息支付”+“本金支付”+“当期余额”);

for(int i=1;i对于for的所有迭代,您都给出了相同的值。这就是为什么您总是得到相同的输出

    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
如果每次都需要新的值,可以提示用户为每次迭代提供新的输入,也可以在for之前这样做,将输入存储在数组中,并在循环中处理它们

    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
类似于以下伪代码:

    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
for(i = 0; i < N; i++)
{
    //read principal from user input
    //read payment from user input
    // ect...
    payment_interest=(principal*period_rate);
    ...


}
(i=0;i { //从用户输入读取主体 //从用户输入读取付款 //等等。。。 付款利息=(本金*期间利率); ... }

您可以看到如何从键盘上读取Double。请注意,在循环中,您只需向用户询问可能会变化的参数。

忘记编程。拿上铅笔和纸,在几个月内完成计算。然后将这些计算转换为代码。当您开始编程时,您会超越自己在了解问题所在之前,请先进行rt编码。

尝试更改行:

    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
本金=当前余额

    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
致:

    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
本金=本金-本金支付

    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
或者将循环更改为:

    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
double current_balance = principal;
    for (int i = 1; i <= n; i++)
    {
        double principal_payment = 0;

        double payment_interest;

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



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

    }
双流_余额=本金;

对于(int i=1;i)你的循环中没有任何变化。每次都会得到相同的结果。任何帮助都可以。我对编程很陌生这里的原则是什么?声明在哪里?你在计算“当前_平衡”,但它是循环中的局部(在循环中声明)它的价值不在任何地方使用。你基本上需要做的是更新“主体”在循环中。我刚刚编辑了它。我想在每次循环后找到每次付款的利息。我很了解我的计算,因为我确实使用笔和纸,因为我是新的,我不擅长循环,我卡在那个区域。回到笔和纸,看看你做了什么。给你计算的每个值命名。注意你“循环”的位置在纸上计算,以及你从一个迭代到下一个迭代的值。循环没有什么特别的——这不是一个神奇的公式。你只需要做你在纸上做的事情。真的,你的代码为我工作:输入贷款金额:500输入年利息:50总付款:4输入贷款长度:1利息支付本金付款活期余额62.5 45.28427124023437525.916576385498047 4.127919673919678一个问题,你怎么把它转到。2f