我叫王超然,是一名电脑爱好者,现在在新加坡留学上高一.我立志成为一名电脑人才,愿意在这里与大家一同分享我玩转电脑的心得.O-level华文考了A-One哈哈!
天气: 晴朗
心情: 高兴
/*p1Q9
* Name:Wang Chaoran
* Description:Write a program that reads in investment amount, annual interest rate,
* and number of years, and displays the future investment value using the following formula:
*futureInvestmentValue = investmentAmount x (1 + monthlyInterestRate)numberOfYears*12
*For example, if you entered amount 1000, annual interest rate 3.25%, and number of years 1, the future investment value is 1032.98.
*Hint: Use the Math.pow(a, b) method to compute a raised to the power of b.
*/
import java.util.Scanner;
import java.text.DecimalFormat;
class p1Q9
{
public static void main(String[] args)
{
double investmentAmount, annualInterestRate, numberOfYears, monthlyInterestRate, futureInvestmentValue;
System.out.println("Please in put the investment amount: ");
//Step One:Create a scanner
Scanner scan = new Scanner(System.in);
//Step Two:Read in investmentAmount, monthlyInterestRate and numberOfYears
investmentAmount = scan.nextDouble();
System.out.println("Please in put the annual interest rate: ");
annualInterestRate = scan.nextDouble();
System.out.println("Please in put the number of years: ");
numberOfYears = scan.nextDouble();
//Step Three: Calculate future investment value
monthlyInterestRate = annualInterestRate /12;
futureInvestmentValue = investmentAmount * Math.pow((1 + monthlyInterestRate),numberOfYears*12);
DecimalFormat df = new DecimalFormat("0.00");
String output = df.format(futureInvestmentValue);
/*altenate method: futureInvestmentValue*1000 =futureInvestmentValue;
if ((futureInvestmentValue % 10)>=5)
{
futureInvestmentValue =Math.ceil(futureInvestmentValue / 10);
}
else futureInvestmentValue = Math.rint(futureInvestmentValue/10);
futureInvestmentValue = futureInvestmentValue/100;*/
//refer to 5.9. The Math Class
//Step Four: Display future investment rate
System.out.println("The future investment value is: "+output);
}
}
导入论坛查看(33)回复(0)引用(0)好评(0) 差评(0)
加入收藏
编辑
审核
TAG:
computing