Java 使用Math.pow方法
我正在为我的Java编程入门课程做一个项目,在这个项目中,我必须创建一个计算用户未来投资价值的程序。程序中必须提示用户输入三项内容:投资金额、年利率和投资年限。有了这些信息,程序应该能够计算出用户的月利率,进而计算出他们未来的投资价值 让我们从我教授的未来投资公式开始:Java 使用Math.pow方法,java,pow,Java,Pow,我正在为我的Java编程入门课程做一个项目,在这个项目中,我必须创建一个计算用户未来投资价值的程序。程序中必须提示用户输入三项内容:投资金额、年利率和投资年限。有了这些信息,程序应该能够计算出用户的月利率,进而计算出他们未来的投资价值 让我们从我教授的未来投资公式开始: futureInvestmentValue = investmentAmount x (1 + monthlyInterestRate)^numberOfYears* 12 下面是我目前的代码: public static v
futureInvestmentValue = investmentAmount x (1 + monthlyInterestRate)^numberOfYears* 12
下面是我目前的代码:
public static void main(String[] args) {
// Create scanner objects for investmentAmount, numberOfYears, and annualInterestRate
Scanner investInput = new Scanner(System.in);
Scanner rateInput = new Scanner(System.in);
Scanner yearInput = new Scanner(System.in);
// Declare variables
int investmentAmount, numberOfYears;
double annualInterestRate, rate, monthlyRate, futureInvestmentValue;
// Create user inputs for investmentAmount, numberOfYears, and annualInterestRate
System.out.print("Please enter your investment amount: ");
investmentAmount = investInput.nextInt();
System.out.print("Please enter your annual interest rate: ");
annualInterestRate = rateInput.nextInt();
System.out.print("Please enter the number of years for your investment: ");
numberOfYears = yearInput.nextInt();
// Variable assignments
rate = annualInterestRate / 100;
monthlyRate = rate / 12;
futureInvestmentValue = investmentAmount * (1.0 + monthlyRate);
//Output
System.out.print("Your annual interest rate is " + rate +
" and your monthly interest rate is " + monthlyRate);
investInput.close();
rateInput.close();
yearInput.close();
}
我根据用户的输入计算了用户的月利率,并开始将教授的公式翻译成Java语言。但是,我不知道如何使用Math.pow方法来转换教授方程的指数部分
// if you want e^b:
double result = Math.exp(b);
// if you want a^b:
double result = Math.pow(a, b);
别忘了:
import java.lang.Math;
别忘了:
import java.lang.Math;
公式可以翻译为Java,如下所示:
double duration = numberOfYears * 12
double futureInvestmentValue = investmentAmount * Math.pow((1 + monthlyInterestRate), duration)
公式可以翻译为Java,如下所示:
double duration = numberOfYears * 12
double futureInvestmentValue = investmentAmount * Math.pow((1 + monthlyInterestRate), duration)
以下是如何使用
Math.pow()
:
Math.pow(x,y);//x^y
其中x=(1+月利率)
y=numberOfYears*12这是如何使用
Math.pow()
:
Math.pow(x,y);//x^y
其中x=(1+月利率)
y=numberOfYears*12请删除所有与
Math.Pow
部分无关的代码以及所有与之无关的介绍性文本。我们不想知道整个任务;我们想在不到5行的代码中看到确切的问题所在。您是说想知道调用Math.pow
的语法吗?或者你是说你不知道该怎么做?您可能会从中获得一些见解是的,我想知道调用Math.pow的语法。大卫发布的链接似乎是我需要看到的,谢谢。下一次我会尽量不要这么啰嗦。是的,在不删除所有不相关的东西的情况下,我们会开始指出一些事情,比如你不需要3台扫描仪,只需要一台,你会打3次电话,而不管你的数学问题是什么。。,,请删除所有与Math.Pow
部分无关的代码以及所有不相关的介绍性文本。我们不想知道整个任务;我们想在不到5行的代码中看到确切的问题所在。您是说想知道调用Math.pow
的语法吗?或者你是说你不知道该怎么做?您可能会从中获得一些见解是的,我想知道调用Math.pow的语法。大卫发布的链接似乎是我需要看到的,谢谢。下一次我会尽量不要这么啰嗦。是的,在不删除所有不相关的东西的情况下,我们会开始指出一些事情,比如你不需要3台扫描仪,只需要一台,你会打3次电话,而不是你的数学问题。pow问题是……,这是一个非常具体的答案;多谢各位+一个好答案。非常小的一点-我会使用名称numberOfMonths
,而您使用了duration
,只是为了让它更清楚一点。这是一个非常具体的答案;多谢各位+一个好答案。非常小的一点-我会使用名称numberOfMonths
,您使用的是持续时间
,只是为了让它更清楚一点。