简体   繁体   English

For 循环向后递增数组

[英]For loop increments array backwards

I am trying to do some calculations using different values stored in an array.我正在尝试使用存储在数组中的不同值进行一些计算。 The problem is that my array's values increase but the calculations are coming out decreasing.问题是我的数组的值增加了,但计算结果却在减少。

Main:主要的:

public static void main(String[] args) {
    double pay, rate, deposit;
    int years;
    char yes = 'y';
    char answer;
    double[] interest = new double[15];
    interest[0] = 3.75;
    interest[1] = 4.00;
    interest[2] = 4.25;
    interest[3] = 4.50;
    interest[4] = 4.75;
    interest[5] = 5.00;
    interest[6] = 5.25;
    interest[7] = 5.50;
    interest[8] = 5.75;
    interest[9] = 6.00;
    interest[10] = 6.25;
    interest[11] = 6.50;
    interest[12] = 6.75;
    interest[13] = 7.00;
    interest[14] = 7.25;

    mortgageClass mortgagePayment = new mortgageClass();

    Scanner keyboard = new Scanner(System.in);

    System.out.print("Are you a first time buyer? ");
    answer = keyboard.next().charAt(0);
    if (answer == yes)
    {
        mortgagePayment.setRate(4.50);
    }
    else
    {
        mortgagePayment.setRate(interest[0]);
    }

    System.out.print("What is your mortage term? ");
    years = keyboard.nextInt();
    mortgagePayment.setTermYears(years);

    System.out.print("What is your amount of mortgage? ");
    pay = keyboard.nextDouble();
    mortgagePayment.setAmount(pay);

    System.out.print("What is your deposit amount? ");
    deposit = keyboard.nextDouble();
    mortgagePayment.setdepositAmt(deposit);

    System.out.printf("Your mortgage payment is %.2f ", mortgagePayment.getMonthlyPayment());
    System.out.println();

    for ( int i = 0; i < interest.length; i++ ){
        mortgagePayment.setRate(interest[i]);
        System.out.printf("Your mortgage payment is %.2f ", mortgagePayment.getMonthlyPayment());
        System.out.println();
    }
}

MortgagePayment Class按揭付款类别

public class mortgageClass {

    private double rate;
    private double loanAmount;
    private double depositAmt;
    private int termYears;

    public void setRate(double r) {
        rate = r;
    }

    public void setAmount(double loan) {
        loanAmount = loan;
    }

    public void setTermYears(int years) {
        termYears = years;
    }

    public void setdepositAmt(double amount) {
        depositAmt = amount;
    }

    public double getMonthlyPayment() {
        rate /= 100.0;
        loanAmount = loanAmount - depositAmt;
        double monthlyRate = rate / 12.0;
        int termMonths = termYears * 12;
        double monthlyPayment = (loanAmount * monthlyRate)
                / (1 - Math.pow(1 + monthlyRate, -termMonths));
        return monthlyPayment;
    }
}

The output is decreasing产量在减少

Your mortgage payment is 1309.00 
Your mortgage payment is 1220.49 //my output
Your mortgage payment is 1128.42 

When it should be increasing什么时候应该增加

Your mortgage payment is 1309.00 
Your mortgage payment is 1331.44 //Expected output
Your mortgage payment is 1354.10

Obviously the first value is assigned correctly, so why isn't the increment working?显然第一个值被正确分配,那么为什么增量不起作用?

EDIT: I have added a print statement to see if the correct values are being used and it seems like they are编辑:我添加了一个打印语句来查看是否使用了正确的值,看起来它们是

  for ( int i = 0; i < interest.length; i++ ){
    mortgagePayment.setRate(interest[i]);
    //System.out.printf("Your mortgage payment is %.2f ", mortgagePayment.getMonthlyPayment());
    System.out.println(interest[i]);
    System.out.printf("Your mortgage payment is %.2f ", mortgagePayment.getMonthlyPayment());
    System.out.println();

Output:输出:

3.75
Your mortgage payment is 1309.00 
4.0
Your mortgage payment is 1220.49 
4.25
Your mortgage payment is 1128.42 
4.5
Your mortgage payment is 1032.74 

..... I would just like to know WHY the calculations are decreasing. ..... 我只想知道为什么计算量在减少。

skip跳过

interest[i] = i + 1;

EDITED 2nd time :第二次编辑:

the problem is问题是

loanAmount = loanAmount - depositAmt;

every time you call mortgagePayment.getMonthlyPayment()每次你调用mortgagePayment.getMonthlyPayment()

it decreasing the loanAmount , Thats why monthly amount is coming down它减少了贷款金额,这就是为什么每月金额下降的原因

suggested change :建议更改:

 public double getloanAmount(){

        return loanAmount - depositAmt;
    }

    public double getMonthlyPayment() {
        rate /= 100.0;

        double loanAmount = getloanAmount();

        double monthlyRate = rate / 12.0;

        int termMonths = termYears * 12;

   double monthlyPayment = (loanAmount * monthlyRate) / (1 - Math.pow(1 + monthlyRate, -termMonths));

        return monthlyPayment;
    }

that will fix the problem.这将解决问题。

EDITED : use this fromula Monthly Mortgage Payment编辑:使用这个 fromula Monthly Mortgage Payment

M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]

The variables are as follows:

    M = monthly mortgage payment
    P = the principal, or the initial amount you borrowed.
    i = your monthly interest rate. Your lender likely lists interest rates as an annual figure, 
        so you’ll have to divide by 12, for each month of the year. So, if your rate is 5%, 
        then the monthly rate will look like this: 0.05/12 = 0.004167.
    n = the number of payments, or the payment period in months. If you take out a 30-year fixed rate mortgage, 
        this means: n = 30 years x 12 months per year = 360 payments.

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM