如何获得正确的输出?

时间:2015-02-15 23:42:39

标签: java math

所以对于这个,我似乎只获得了输出的美元数量的“无限”。我认为这与数学有关。 (不要介意草率的代码,我被告知调试它而不会过多地改变它)这是我似乎唯一的问题。除此之外,其他一切工作正常。

/* Chapter 3 Debugging Assignment
 *Programmer:
 *Date:
 *Program Name: Bert,java
 *Purpose:
 */

import java.util.Scanner;

public class bert {

    public static void main(String[] args) {

        //Declaring Variables
        int price, downPayment, tradeIn, months, loanAmt, interest;
        double annualInterest, payment;
        String custName;

        Scanner reader = new Scanner(System.in);

        //Get Input from User
        System.out.println("What is your first name? ");
        custName = reader.next();
        System.out.print("What is the price of the car? ");
        price = reader.nextInt();
        System.out.print("What is the downpayment? ");
        downPayment = reader.nextInt();
        System.out.print("What is the trade-in value? ");
        tradeIn = reader.nextInt();
        System.out.print("For how many months is the loan? ");
        months = reader.nextInt();
        System.out.print("What is the decimal interest rate? ");
        annualInterest = reader.nextDouble();

        //Output
        calculatePayment(price, downPayment, tradeIn, (int) annualInterest, months);
    }

    public static void calculatePayment(int price, int downPayment, int tradeIn,
            int months, double annualInterest) {

        double interest;
        int loanAmt;
        double payment;

        //Calculations
        interest = (double) annualInterest/ (double) 12 ;
        loanAmt = (int) (price - downPayment - tradeIn);
        payment = loanAmt / ((1.0 / interest) - (1.0 / (interest * Math.pow(1.0, annualInterest))));
        String custName = "Any Name";

        System.out.print("The monthly payment for ");
        System.out.print(custName + " is $ ");
        System.out.println((double) payment);

        return;
    }
}

1 个答案:

答案 0 :(得分:0)

calculatePayment的参数不匹配。
计算付款(price,downPayment,tradeIn,(int)annualInterest,months);应为
计算付款(价格,下付,贸易,<强>月,年度利息);你的数学很糟糕。
Math.pow(1.0,annualInterest) 总是 1.
所以你有< em> 1 / interest-1 / interest 为0.那么你有loanAmt / 0.0 ==&gt; 无限
返回并查看付款计算