每月付款计算器

时间:2012-10-01 02:39:23

标签: java logic

我有一些代码,我发现不断给我一个除以0的错误。 假设计算每月付款金额!

import java.io.*;

public class Bert
{
public static void main(String[] args)throws IOException
{
    //Declaring Variables
    int price, downpayment, tradeIn, months,loanAmt, interest;
    double annualInterest, payment;
    String custName, inputPrice,inputDownPayment,inputTradeIn,inputMonths, inputAnnualInterest;
    BufferedReader dataIn = new BufferedReader(new InputStreamReader(System.in));

   //Get Input from User
    System.out.println("What is your name?  ");
    custName = dataIn.readLine();
    System.out.print("What is the price of the car?  ");
    inputPrice = dataIn.readLine();
    System.out.print("What is the downpayment?  ");
    inputDownPayment = dataIn.readLine();
    System.out.print("What is the trade-in value?  ");
    inputTradeIn = dataIn.readLine();
    System.out.print("For how many months is the loan?  ");
    inputMonths = dataIn.readLine();
    System.out.print("What is the decimal interest rate?  ");
    inputAnnualInterest = dataIn.readLine();

    //Conversions
    price = Integer.parseInt(inputPrice);
    downpayment = Integer.parseInt(inputDownPayment);
    tradeIn = Integer.parseInt(inputTradeIn);
    months = Integer.parseInt(inputMonths);
    annualInterest = Double.parseDouble(inputAnnualInterest);




            interest =(int)annualInterest/12;
            loanAmt = price-downpayment-tradeIn;

            //payment = loanAmt*interest/a-(1+interest)
            payment=(loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,-months)))));
    //Output
    System.out.print("The monthly payment for " + custName + " is $");
    System.out.println(payment);
            // figures out monthly payment amount!!!
}
}

尝试设置付款变量时出现问题。 我不明白为什么它不断出现0除错。

4 个答案:

答案 0 :(得分:2)

您已将变量声明为Int,因此1/interest1/(interest*Math.pow(1+interest,-months))将返回0.将变量的类型更改为float或double。

答案 1 :(得分:0)

向您提出的一个建议是,您应该学会“反向切片”您的代码。

这意味着当您看到自己获得DivideByZeroException后,您应该查看您的代码,并说“为什么会发生这种情况?”

在你的情况下,让我们来看看:

payment=(loanAmt/((1/interest)-(1/(interest*Math.pow(1+interest,-months)))));

所以,现在,Math.pow永远不会返回任何零(因为它是一个幂),所以必须是interest为零的情况。让我们找出原因:

interest =(int)annualInterest/12;

现在,Java 中的整数除法会截断。这意味着如果你有.5它将被切断,并变为零。 (类似地,1.3将被截断为0)。

现在:

annualInterest = Double.parseDouble(inputAnnualInterest);

这意味着你传入的东西被解析为小于12的值。如果它大于12,那么你会得到别的东西。

但是,您可能只是传入一个无效的字符串,例如,传入"hello2.0"将无效!

答案 2 :(得分:0)

这将总是四舍五入为0.因此它会抛出异常。

   (1/interest)-(1/(interest*Math.pow(1+interest,-months))))); 

使用float类型代替int。了解它们的工作原理。

答案 3 :(得分:-1)

package computeloan;




import java.util.Scanner;

public class ComputeLoan {

public static void main(String[] args) {
   Scanner input = new Scanner(System.in);

        System.out.print(" Enter Yearly Interest Rate : ");

        double annualIntersetRate = input.nextDouble();




        double monthlyIntersetRate = annualIntersetRate / 1200;




        System.out.print(" Enter Number of years :  ");


        int numberOfYears = input.nextInt();


        // Enter loan amount


        System.out.print(" Enter Loan Amount : ");


        double loanAmount = input.nextDouble();


        double monthlyPayment = loanAmount * monthlyIntersetRate /(1-1/Math.pow(1+monthlyIntersetRate,numberOfYears*12 ));





        double totalPayment = monthlyPayment * numberOfYears * 12;
        //Calculate monthlyPaymeent and totalPayment

        System.out.println(" The Monthly Payment Is : " +(int)(monthlyPayment*100) /100.0);





        System.out.println(" The Total Payment Is : " +(int)(totalPayment*100) /100.0 );
    }
}