我有一個複利計算器,但是當我運行的代碼,輸入下面的數字時,它要求它:複利計算器不給我正確的數據?
校長:10000 率:0.02 年:10
,並選擇「年」它已經被設置好了,所以如果我或者用戶輸入那個特定的字符串,選擇變量將自動變爲1(或者如果我輸入季度或者月份的話,其他值已經設置)。然而,我應該得到的價值:$ 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);
我使用的公式是由我的教授提供的,用於我必須創建的代碼以及我必須專門使用的代碼。此外,我事先用手和計算器檢查了它,並獲得了我應該得到的價值。但是,當我運行代碼時,特定的結果不會消失。 – Huntress
您可能錯誤地實施了教授的配方。你的例子中的代碼計算:'10000 *(1+(0.02/1))*(1^10)= 10200'。你想要做'10000 *((1+(0.02/1))^(1 * 10))= 12189'。試試我提供的代碼,看看它是否工作。 – tfogo
嘗試過,但它仍然給我錯誤的輸出。 – Huntress