简体   繁体   English

如何在 Java 中实现特定的数学公式?

[英]How to implement a particular maths formula in Java?

package methods;
import java.util.Scanner;

/**
*
* @author Billy
*/
public class Methods {

/**
 * @param args the command line arguments
 */
    public static void main(String[] args) {      
    double Loanamount; // Loan amount from user
    double Aintrate; // User's annual interest rate
    double months; // Number of months on loan
    double monp; // Monthly payment
    double Mintrate; // Monthly interest rate
    double n; // The number of payments


    // declare an instance of Scanner to read the datastream from the keyboard.
    Scanner kb = new Scanner(System.in);

    // Get user's loan amount
    System.out.print("Please enter your total loan amount: ");
    Loanamount = kb.nextDouble();

    // Get user's interest rate
    System.out.print("Please enter your annual interest rate as a decimal: ");
    Aintrate = kb.nextDouble();

    // Get user's number of months
    System.out.print("Please enter the number of months left on your loan: ");
    months = kb.nextDouble();

    // Calculate montly interest rate
    Mintrate = ((Aintrate / 12));

       System.out.println("Your monthly interest rate is " + " " + Mintrate);

    // Calculate number of payments
       n = ((months * 12));

    // Calculate monthly payment

My next job is to find out the monthly payment using this formula.我的下一个工作是使用这个公式找出每月的付款

M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1] M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]

Where在哪里

M = monthly mortgage payment M = 每月按揭付款
P = The amount borrowed (Loanamount) P = 借入的金额(Loanamount)
i = Monthly interest rate (Mintrate) i = 月利率(Mintrate)
n = the number of payments n = 支付次数

I tried the following but I just cant figure it out我尝试了以下但我无法弄清楚
monp = Loanamount [Mintrate(1+ Mintrate)^n] / [(1+ Mintrate)^n-1 ];

Math.pow is what you need instead of ^ . Math.pow是您所需要的,而不是^ Also you can't use [ or ] .你也不能使用[] You have to use parenthesis ( and ) .您必须使用括号( and ) More math functions can be found at: http://docs.oracle.com/javase/7/docs/api/java/lang/Math.html可以在以下位置找到更多数学函数: http : //docs.oracle.com/javase/7/docs/api/java/lang/Math.html

You need to use Math.pow effectivley here.你需要在这里使用Math.pow effectivley。

Try using following in your code尝试在您的代码中使用以下内容

monp = Loanamount*(Mintrate*Math.pow((1+ Mintrate),n)) / (Math.pow((1+ Mintrate),n-1 ) ;

The method for ^ is called Math.pow(double a, double b); ^的方法称为Math.pow(double a, double b); in Java.在爪哇。

Where a is the number to be raised b times.其中a是要提高b次的数字。

Your formula would then look like monp = Loanamount Math.pow((Mintrate(1+ Mintrate), n)) / (Math.pow((1+ Mintrate), n-1));你的公式看起来像monp = Loanamount Math.pow((Mintrate(1+ Mintrate), n)) / (Math.pow((1+ Mintrate), n-1));

Reference 参考

You need to call Math.pow(double,double)你需要调用 Math.pow(double,double)

OR you could import the required class as..或者您可以将所需的类导入为..
import static java.lang.Math.pow;
and then use pow() function然后使用 pow() 函数

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM