2017-10-15 8 views
2

私は、ユーザーが必要とする投資の種類(年、月、または四半期)を指定できるコードを作成しており、各投資タイプは特定の整数、つまり年次= 1、月次= 12、四半期= 4しかし、私が年に値を割り当てたとき、私は以下の私の投資方程式のint値に関連付ける必要もあり、その方法を完全に困惑しています。割り当てられた文字列値をInteger値に関連付ける方法は?

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); 
    } 
+0

// saving = principal * (1 + (rate/plan.term)) * Math.pow(plan.term, years); saving = plan.calculateSavings(principal, rate,years); 
[Integer.parseInt (年)] = your_correlated_value; – Jar3d

答えて

1
  • 投資計画の種類が回答されたら、quest変数は、あなたが期待している文字列、すなわち、AnnualQuarterly、またはMonthlyのいずれかと一致するかどうかを確認する必要があります。
  • quest試合の選択肢のいずれかの場合は、すなわち、1、4、または答えがない場合にも、また、状況を考える必要があるかもしれません12
  • choice変数に正しい値を代入正しい選択肢のいずれかにマッチします。

    if ("Annual".equalsIgnoreCase(quest)) { 
        choice = 1; 
    } else if ("Quarterly".equalsIgnoreCase(quest)) { 
        choice = 4; 
    } else if ("Monthly".equalsIgnoreCase(quest)) { 
        choice = 12; 
    } else { 
        //you need to do something here. 
    } 
    
0

私はあなたがしたいint型を定義する列挙型を使用することをお勧めします。 (これはint型の選択を置き換える)あなたはこのようなあなたのコードでこれを使用することになり

public enum Plan { 
    ANNUAL(1), 
    QUARTERLY(4), 
    MONTHLY(12); 

    int term; 

    Plan(int term) { 
     this.term = term; 
    } 
}; 

:私は、列挙型のプランを呼び出し、int型の用語ます

Plan plan = Plan.valueOf(quest.toUpperCase()); 
    saving = principal * (1 + (rate/plan.term)) * Math.pow(plan.term, years); 

私はあなたが別の必要としていると思いますあなたの計算のバージョン。列挙型アプローチは、列挙型の値を切り替えるメソッドを列挙型に追加すると、これを簡単にサポートします。計算のさまざまな実装を試して、case文で定義することができます。

double calculateSavings(int principal, double rate, int years) { 
     switch (this) { 
      case ANNUAL: 
      case QUARTERLY: 
      case MONTHLY: 
      default: 
       return principal * (1 + (rate/term)) * Math.pow(term, years); 
     } 
    } 

あなたはこのルートは、あなたがこのようなあなたのコードでそれを使用して行く場合:

あなたはそのinvesmentような位置年次、月次や四半期の配列「invesment」を作成することができます
関連する問題