본문 바로가기
Java/연습문제

[JAVA] 원금 2배되는 기간

by 2D3 2022. 10. 21.
728x90

문제: 원금 2배되는 기간

 

해결 과정

기간이 되는 것 선언 -> 빠진 것: 연이율, 원금

if문 사용해서 2배가 되는 조건 걺 -> if문이 아니라 while을 사용해서 조건이 만족되면 year을 올려야 됨 -> 조건: 원금*연이율이 2배가 되기 전까지

 

구현

public class Main {
    public int computeWhenDouble(double interestRate) {
        // TODO:
        double rate = 1 + interestRate / 100; //연이율
        double principal = 1; 			//원금
        int year = 0; 				//기간

        while (principal < 2){
            principal = principal * rate;
            year++;
        }
        return year;
    }
}

 

728x90

댓글