2017-10-15 2 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 변수가 당신이 기대하는 문자열, 즉, ​​Annual, Quarterly, 또는 Monthly의 일치 여부를 확인해야합니다.
  • quest이 선택의 일치하는 경우, 당신은 4, 1, 즉, choice 변수에 올바른 값을 지정하거나 12
  • 또한도 대답하지 않는 경우는 상황을 생각해야 할 수도 있습니다

    올바른 선택 항목 중 하나와 일치해야합니다.

    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로 정의하는 열거 형을 사용하는 것이 좋습니다 것입니다. 난 당신이 다른 필요하려고 생각

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

:

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

    int term; 

    Plan(int term) { 
     this.term = term; 
    } 
}; 
당신은이 같은 코드에서 이것을 사용하는 것이

(이것은 INT 선택을 대체) : 나는 열거 계획과 INT 용어를 전화 할게 귀하의 계산 버전. 열거 형 접근법은 열거 형의 값을 전환하는 열거 형에 메서드를 추가 한 경우이를 쉽게 지원합니다. 계산의 다른 구현을 연구하고 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"를 만들 수 있습니다