我正在編寫一個代碼,允許用戶決定他們想要什麼類型的投資(年度,月度或季度),並且每種投資類型都與特定的整數相關聯:即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