【问题标题】:For loop increments array backwardsFor循环向后递增数组
【发布时间】:2015-06-24 04:53:20
【问题描述】:

我正在尝试使用存储在数组中的不同值进行一些计算。问题是我的数组的值增加了,但计算出来的却减少了。

主要:

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 类

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;
    }
}

输出在减少

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

什么时候应该增加

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

显然第一个值是正确分配的,那么为什么增量不起作用?

编辑:我添加了一个打印语句,以查看是否使用了正确的值,并且看起来它们是

  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();

输出:

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 

.....我只想知道为什么计算在减少。

【问题讨论】:

  • 你为什么要interest[i] = i + 1;?还有setRategetMonthlyPayment 有什么作用?
  • 您需要向我们展示MortgagePayment 类的代码。
  • @TimBiegeleisen 我已经添加了整个程序
  • 你用来生成上面给出的输出的输入值是什么?
  • @TimBiegeleisen 您是第一次购买吗? n 您的抵押贷款期限是多少? 15 您的抵押贷款金额是多少? 195000 您的存款金额是多少? 15000

标签: java arrays loops for-loop


【解决方案1】:

跳过

interest[i] = i + 1;

第二次编辑:

问题是

loanAmount = loanAmount - depositAmt;

每次拨打mortgagePayment.getMonthlyPayment()

它减少了loanAmount,这就是为什么每月的金额会下降

建议更改:

 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;
    }

这将解决问题。

编辑: 使用这个 fromula 每月按揭付款

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.

【讨论】:

  • 我已经尝试过了,它会产生完全相同的输出
  • 问题出在别的地方,即使我提出的公式是一样的。在回答中讨论问题
  • 成功了!你能向我解释一下mortgagePayment 类的变化究竟做了什么吗?
  • 我刚刚添加了 getloanAmount() 方法。并删除了行“loanAmount = loanAmount - depositAmt;”来自 getMonthlyPayment() 并添加了“double loanAmount = getloanAmount();”进入相同的方法(getMonthlyPayment())
猜你喜欢
  • 1970-01-01
  • 2013-01-12
  • 2019-01-17
  • 2017-04-29
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-04-12
相关资源
最近更新 更多