-3

我有一些代码,我发现它一直给我一个除以 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

4 回答 4

2

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

于 2012-10-01T02:44:04.043 回答
0

给你的一个建议是,你应该学会“向后切片”你的代码。

这意味着当你看到你得到 a 时,DivideByZeroException你应该看看你的代码,然后说,“为什么会发生这种情况?”

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

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

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

interest =(int)annualInterest/12;

所以现在,Java 中的整数除法会截断。这意味着如果您有 0.5,它将被切断,并变成零。(同样,1.3 将被截断为 0)。

所以现在:

annualInterest = Double.parseDouble(inputAnnualInterest);

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

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

于 2012-10-01T02:50:43.623 回答
0

这将始终四舍五入为 0。所以它会引发异常。

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

使用float类型而不是int。了解它们的工作原理。

于 2012-10-01T02:55:35.860 回答
-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 );
    }
}
于 2014-02-16T23:46:59.667 回答