首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >当我做数学的时候,Java总是想绕圈子。

当我做数学的时候,Java总是想绕圈子。
EN

Stack Overflow用户
提问于 2014-02-17 04:12:08
回答 3查看 120关注 0票数 1

问题正在发生,我认为,在那里,该计划使用利率。使用测试数据:贷款额:50万美元年利率: 5.6%年数:30年

我应该得到结果:每月付款:2,870.39美元

相反,我得到:贷款额:50万美元年利率: 6%年数: 30.0月还款:34,783.54美元

请帮帮忙

编辑-这是一个家庭作业作业,教授已经指出,他不希望我们使用BigDecimal对象。为什么是我无法理解的,因为我已经证明了我在过去用BigDecimal数学解决这个问题的能力。

以下是代码:

代码语言:javascript
复制
/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */

package workbook5.pkg2;
import java.util.Scanner;
import java.text.*;
/**
 *
 * @author Daniel
 */
public class Workbook52 {

    /**
 * @param args the command line arguments
 */
public static void main(String[] args) {
    // TODO code application logic here
    Scanner sc = new Scanner(System.in);
    double loanAmount = getDouble(sc, "Enter loan amount: ", 0, 1000000);
    double interestRate = getDouble(sc, "Enter interest rate (5% as .05): ", 0, 20);
    double years = getInt(sc, "Enter amount of years: ", 0, 100);
    double payment = calculatePayment(loanAmount, interestRate, years);
    System.out.println("FORMATTED RESULTS");
    System.out.println("Loan amount:          " + NumberFormat.getCurrencyInstance().format(loanAmount));
    System.out.println("Yearly interest rate: " + NumberFormat.getPercentInstance().format(interestRate));
    System.out.println("Number of years:      " + years);
    System.out.println("Monthly Payment:      " + NumberFormat.getCurrencyInstance().format(payment));
}
public static double getDouble(Scanner sc, String prompt, double min, double max){
    double i = 0;
    boolean isValid = false;
    while (isValid == false)
    {    
        System.out.println(prompt);
        if (sc.hasNextDouble())
        {
          i = sc.nextDouble();
          isValid=true;
        }


        else
        {
            System.out.println("Error! Invalid double value. Try again.");

        }
        sc.nextLine();
        if (isValid == true && i <= min)
        {
            System.out.println("Error: Number must be greater than " + min);
            isValid = false;
        }
        else if (isValid == true && i >= max)
        {
            System.out.println("Error: Number must be less than " + max);
            isValid = false;
        }
    }
    return i;
}

public static int getInt(Scanner sc, String prompt, int min, int max){
    int i = 0;
    boolean isValid = false;
    while (isValid == false)
    {
        System.out.println(prompt);
        if (sc.hasNextInt())
        {
          i = sc.nextInt();
          isValid=true;
        }        
        else
        {
            System.out.println("Error! Invalid integer value. Try again.");

        }
        sc.nextLine();
        if (isValid == true && i <= min)
        {
            System.out.println("Error: Number must be greater than " + min);
            isValid = false;
        }
        else if (isValid == true && i >= max)
        {
            System.out.println("Error: Number must be less than " + max);
            isValid = false;
        }
    }    

    return i;
    }






 public static double calculatePayment(double amount, double interest, double years) {
       double monthlyPayment = amount * interest/(1 - 1/Math.pow(1 + interest, years));
       return monthlyPayment;
   }
}
EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2014-02-17 04:33:00

在你的程序中舍入没有什么问题。对你公式的输入是错误的。你用年利率和年数来计算月还款。你需要转换成一个月利率和条款的数目。试试这个:

代码语言:javascript
复制
public static double calculatePayment(double amount, double interest, double years) {
    double monthlyInterest = interest / 12.0;
    double terms = years * 12.0;
    double monthlyPayment = amount * monthlyInterest
            / (1 - 1 / Math.pow(1 + monthlyInterest, terms));
    return monthlyPayment;
}
票数 0
EN

Stack Overflow用户

发布于 2014-02-17 04:15:34

每当您在java中处理money时,您使用的数据类型是BigDecimal,而不是浮点或双值。修改你的程序使用BigDecimal而不是双倍,你应该得到正确和预期的结果。

票数 1
EN

Stack Overflow用户

发布于 2014-02-17 04:28:54

如果以美分存储和计算所有货币值,则可以安全地使用int (或long用于非常大的金额)。

银行业(特别是EFTPOS)使用美分,如果它对他们来说足够好的话,你也可以接受。

您只将该数字呈现为具有货币符号的美元的十进制数。

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/21820598

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档