2017-10-15 6 views
0

私は複利計算機を持っていますが、私は、コードと入力、次の数字を実行すると、それはそれを要求する場合:Compound Interest calculatorが正しいデータを与えていないのですか?

校長:10000 率:0.02 年:10

と選択し、「年次」これは既に設定されているため、Iまたはユーザーがその特定の文字列を入力すると、選択変数は自動的に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); 

答えて

0

あなたの計算式は間違っています。それは次のようになります。

saving = principal*Math.pow(1+(rate/choice), choice*years); 

はここに正しい式を参照してください:https://en.wikipedia.org/wiki/Compound_interest#Periodic_compounding

+0

私が使用している式は、私が作成しなければならないこのコードと私が特に使用しなければならないものの、私の教授によって提供されたものです。プラス私は手で、電卓でそれを事前にチェックして、私が得なければならない価値を得ました。しかし、コードを実行すると、特定の結果が乱れることはありません。 – Huntress

+1

教授の数式が正しく実装されていない可能性があります。あなたの例のコードは '10000 *(1+(0.02/1))*(1^10)= 10200'です。あなたは '10000 *((1+(0.02/1)^(1 * 10))= 12189'をしたいと思っています。私が提供したコードを試して、それが動作するかどうか確認してください。 – tfogo

+0

それを試みましたが、それでも私は間違った出力を与えています。 – Huntress

1

は提案を提供するためのみんなに感謝します。ちょうど私が使用する必要があると思われた:

saving = principal * Math.pow(1+(double) (rate/ (choice)), choice * years); 

私の方程式が動作するためには、貯蓄が二重に分類されるように私の式が適切に考慮に私の整数値を取っていなかったようだと。

関連する問題