您好我正在尝试编写一个使用固定费率计算未来投资价值的程序,而我的变量存在问题。任何帮助都会非常感谢你提前谢谢!
/**
* NAME:
* DATE: November 3, 2015
* FILE:
* COMMENTS: This program caculates a future investment value using a fixed rate
*/
import java.util.Scanner;
public class lab12
{
public static void main(String[] args)
{
//declare variables
double yearlyInterstRate;
double monthlyInterestRate;
double monthlyInvestmentAmount;
double years;
double months;
//Create Scanner Object
Scanner keyboard= new Scanner(System.in);
//Get Investment Information
System.out.print("Hello we will be caculating the future of your fixed monthly investment, I will need to collect some information from you.");
System.out.print("What is your monthly investment amount?");
monthlyInvestmentRate=keyboard.nextLine();
System.out.print("How many years wil you be investing for?");
years=keyboard.nextLine();
System.out.print("What is your yearly interest rate?");
yearlyInterestRate=keyboard.nextLine();
//Caculate the future rate
{
months=years*12;
monthlyInterestRate=yearlyInterestRate/12/100;
futureValue= CaculateFuturevalue(monthlyInvestedAmount,monthlyInterestRate,months);
System.out.print("Your future value is $"+futureValue);
}
} //Close Main
} //close lab12
答案 0 :(得分:1)
你在变量中拼写错误。
您已宣布yearlyInterstRate
,但您正在使用yearlyInterestRate
,因此您需要使用声明的变量。
您也需要使用monthlyInvestmentAmount
而不是monthlyInterestRate
。
答案 1 :(得分:1)
此代码将执行:
public class lab12
{
public static void main(String[] args)
{
//declare variables
double yearlyInterstRate;
double monthlyInterestRate;
double monthlyInvestmentAmount=0; // you forgot to initialize this value
double years;
double months;
double futureValue;
//Create Scanner Object
Scanner keyboard= new Scanner(System.in);
//Get Investment Information
System.out.print("Hello we will be caculating the future of your fixed monthly investment, I will need to collect some information from you.");
System.out.print("What is your monthly investment amount?");
monthlyInterestRate=keyboard.nextDouble();
System.out.print("How many years wil you be investing for?");
years=keyboard.nextDouble();
System.out.print("What is your yearly interest rate?");
yearlyInterstRate=keyboard.nextDouble();
//Caculate the future rate
months=years*12;
monthlyInterestRate=yearlyInterstRate/12/100;
futureValue= caculateFuturevalue(monthlyInvestmentAmount,monthlyInterestRate,months);
System.out.print("Your future value is $"+futureValue);
}
}
现在做了一些改进:
nextDouble()
代替nextLine()
,因为你的变量是双倍的,而不是String。Lab12
而不是lab12