我正在编写一个代码,允许用户决定他们想要什么类型的投资(年度,月度或季度),并且每种投资类型都与特定的整数相关联:即Annual = 1,Monthly = 12和Quarterly = 4 。但是,当我每年分配一个值时,我也需要将它与下面我的投资等式中的整数值相关联,并且完全不知道如何去做。如何将指定的字符串值与整数值相关联?
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("What type of investment plan would you prefer (Annual, Quarterly, or Monthly)?");
String quest =cool.next();
while (quest.equalsIgnoreCase(("Annual")))
{ String Annual="1";
Annual.equals(choice);
}
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();
saving = principal*(1+(rate/choice))* Math.pow(choice, years);
System.out.printf("%.2f", saving);
}
您可以创建一个数组“invesment”,位置年度,月度和季度使invesment [的Integer.parseInt (Annual)] = your_correlated_value; – Jar3d