[英]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
我的下一個工作是使用這個公式找出每月的付款。
M = P [ i(1 + i)^n ] / [ (1 + i)^n – 1]
在哪里
M = 每月按揭付款
P = 借入的金額(Loanamount)
i = 月利率(Mintrate)
n = 支付次數
我嘗試了以下但我無法弄清楚monp = Loanamount [Mintrate(1+ Mintrate)^n] / [(1+ Mintrate)^n-1 ];
Math.pow
是您所需要的,而不是^
。 你也不能使用[
或]
。 您必須使用括號(
and )
。 可以在以下位置找到更多數學函數: http : //docs.oracle.com/javase/7/docs/api/java/lang/Math.html
你需要在這里使用Math.pow
effectivley。
嘗試在您的代碼中使用以下內容
monp = Loanamount*(Mintrate*Math.pow((1+ Mintrate),n)) / (Math.pow((1+ Mintrate),n-1 ) ;
^
的方法稱為Math.pow(double a, double b);
在爪哇。
其中a
是要提高b
次的數字。
你的公式看起來像monp = Loanamount Math.pow((Mintrate(1+ Mintrate), n)) / (Math.pow((1+ Mintrate), n-1));
你需要調用 Math.pow(double,double)
或者您可以將所需的類導入為..
import static java.lang.Math.pow;
然后使用 pow() 函數
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.