Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/heroku/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 余额递减问题_Java_Java.util.scanner_Amortization - Fatal编程技术网

Java 余额递减问题

Java 余额递减问题,java,java.util.scanner,amortization,Java,Java.util.scanner,Amortization,我显示的是每年贷款余额的递减和每年支付的利息。第一年和第二年总是乱七八糟的,导致剩下的几年都不正确。此外,除了像我一样使用空格分隔每个条目之外,我如何打印一列,并为每个条目指定空格 我的代码: int year; periods = loanDurationYears*12; double annualBalance = 0; double annualInterest = 0; int month = loanDurationYears-(loanDurationYears-1);

我显示的是每年贷款余额的递减和每年支付的利息。第一年和第二年总是乱七八糟的,导致剩下的几年都不正确。此外,除了像我一样使用空格分隔每个条目之外,我如何打印一列,并为每个条目指定空格

我的代码:

int year;
periods = loanDurationYears*12;
double annualBalance = 0;
double annualInterest = 0;   
int month = loanDurationYears-(loanDurationYears-1);

            balance = loanAmount;
            double interestForMonth = balance*((interestRate*.01)/12);
            double principalForMonth = monthlyPayment - interestForMonth;
            balance = balance - principalForMonth;
            System.out.println("Annual balances");
            System.out.printf("%s   %s   %s \n","Year","Interest","Balance");

            for(int j=0;j<periods;j++)
            {
                month++;
                year = month/12;

                interestForMonth = balance*((interestRate*.01)/12);
                principalForMonth = monthlyPayment - interestForMonth;
                balance = balance - principalForMonth;
                annualBalance = annualBalance + balance;
                annualInterest = annualInterest + interestForMonth;

                if(month%12 == 0)
                {
                    System.out.printf("%d      %.2f    %.2f \n",year,annualInterest,annualBalance);
                    annualBalance = 0;
                    annualInterest = 0;
                }


            }
输出的外观:除了未对齐的列之外

Year    Interest    Loan Balance
1   5965.23          86408.21   
    2   5715.14      82566.33    
    3   5447.64      78456.94    
    4   5161.51      74061.43    
    5   4855.46      69359.87    
6   4528.10      64330.94    
    7   4177.95      58951.87    
    8   3803.41      53198.26    
    9   3402.80      47044.03    
    10  2974.29      40461.31    
    11  2515.95  33420.24    
    12      2025.70  25888.91    
    13  1501.31  17833.19    
    14  940.40   9216.58    
    15      340.45   -0.00

您得到的起始金额不正确,因为您在for循环之前调整了余额。看起来您的for循环希望从完全平衡开始,但它已经被减少了。您还应该将月份的增量移动到循环的末尾,或者从零开始,而不是从1开始

此行将始终将月份初始化为1。为什么要费心数学呢

int month = loanDurationYears-(loanDurationYears-1);

我还没有测试过这个或任何东西,但它应该可以工作。我相信这不是最好的解决办法,但像这样简单的方法应该可以很好地工作

它只是检查变量的大小,然后根据变量的大小使用不同的打印语句。同时,随着今年的调整,我已经发布了一些代码,应该可以解决这个问题

if(month%12 == 0)
{
    // Set the amount of decimals for all interest rates
    DecimalFormat df = new DecimalFormat("#.##");
    df.format(annualInterest);

    // Get the length of annualInterest
    int n = math.floor(annualInterest);
    int length = (int)(Math.log10(n)+1);

    if (length = 3)
    {
        // Have 1 less space than normal on the print statement
        // Maybe also do a check on the year also as that throws it out when it goes past 10
        if (year > 10)
            System.out.printf("%d     %.2f    %.2f \n",year,annualInterest,annualBalance);
        else
            System.out.printf("%d      %.2f    %.2f \n",year,annualInterest,annualBalance);

    }
    if (length = 2)
    {
        // Have 2 less spaces than normal
    }

    annualBalance = 0;
    annualInterest = 0;
   }

对于间距,您可以从尝试\t制表符而不是多个空格开始。为了让它可靠地排列起来,你必须进行更复杂的计算,因为每个数字的长度可能不同。
if(month%12 == 0)
{
    // Set the amount of decimals for all interest rates
    DecimalFormat df = new DecimalFormat("#.##");
    df.format(annualInterest);

    // Get the length of annualInterest
    int n = math.floor(annualInterest);
    int length = (int)(Math.log10(n)+1);

    if (length = 3)
    {
        // Have 1 less space than normal on the print statement
        // Maybe also do a check on the year also as that throws it out when it goes past 10
        if (year > 10)
            System.out.printf("%d     %.2f    %.2f \n",year,annualInterest,annualBalance);
        else
            System.out.printf("%d      %.2f    %.2f \n",year,annualInterest,annualBalance);

    }
    if (length = 2)
    {
        // Have 2 less spaces than normal
    }

    annualBalance = 0;
    annualInterest = 0;
   }