【发布时间】:2017-10-15 04:20:16
【问题描述】:
我有一个复利计算器,但是当我运行代码并在它要求时输入以下数字时:
本金:10000 率:0.02 年:10
并选择已经设置好的“Annual”,这样如果我或用户输入该特定字符串,则选择变量将自动变为 1(或者如果我输入单词 Quarterly 或 Monthly,则其他值已经设置)。但是,我应该得到的价值是:12189.94 美元,而我得到的价值是:10200.0 我的代码哪里做错了?
import java.util.Scanner;
import java.lang.Math;
public class CompoundInterest {
public static void main (String [] args)
{
Scanner cool = new Scanner (System.in);
double saving, rate;
int principal, years;
int choice;
System.out.println("Please enter you principal investment:");
/*Print statment prompts user to enter their principal investment*/
principal = cool.nextInt();
System.out.println("Would you like to have a regular investment plan?");
/* Print out statement asks user if they would like to participate in a regular investment plan*/
String question =cool.next();
System.out.println("Please enter the number of years that you wish to invest for:");
/* Print statement prompts user to enter the number of years that they wish to invest for*/
years = cool.nextInt();
System.out.println("Please enter the return rate per year:");
/* Print statement prompts user to enter the return rate per year*/
rate = cool.nextDouble();
System.out.println("What type of investment plan would you prefer (Annual, Quarterly, or Monthly)?");
String quest =cool.next();
if ("Annual".equalsIgnoreCase(quest))
{ choice =1;
}
else if ("Quarterly".equalsIgnoreCase(quest)){
choice = 4;
}
else if ("Monthly".equalsIgnoreCase(quest)){
choice = 12;
}
else {
choice = 0;
System.out.println("Please choose a investment plan or speak to a financial advisor.");
}
saving = principal*(1+(rate/choice))*Math.pow(choice,years);
System.out.println(saving);
【问题讨论】:
标签: java