그래서 나는 임금이 일정한 가치에 도달하는 데 걸린 수를 알아 보려고합니다. 예를 들어 제가 5000 달러부터 시작해서 10 % 이자율/년으로 $ 15000을 축적하려고합니다. 나는 이것이 내가 지금까지 무엇을했을되는 투자간단한이자 계산식을 사용하여 Java로 투자 기간 계산하기
의 기간이 얼마나 오래 찾으려
package com.company;
import java.util.Scanner;
public class InvestmentDuration {
public static void main(String[] args) {
Scanner input = new Scanner(System.in);
System.out.println ("Initial Investment: ");
double investment = input.nextDouble();
System.out.println ("Rate as decimals: ");
double rate = input.nextDouble();
System.out.println ("Future Value: ");
double FutureValue = input.nextDouble();
double T = 0; //Initialise Duration//
double EndValue = investment * Math.pow ((1+rate), T); //Formula of Simple Interest//
while (EndValue < FutureValue) {
T += 1.0;
if (investment * Math.pow((1 + rate), T) == FutureValue);
System.out.println("The Number of years it takes to accumulate $" + FutureValue + " is " + T + " years");
}
}
출력 :
The Number of years it takes to accumulate $15000.0 is 1.0 years
The Number of years it takes to accumulate $15000.0 is 2.0 years
The Number of years it takes to accumulate $15000.0 is 3.0 years
The Number of years it takes to accumulate $15000.0 is 4.0 years
The Number of years it takes to accumulate $15000.0 is 5.0 years
The Number of years it takes to accumulate $15000.0 is 6.0 years
The Number of years it takes to accumulate $15000.0 is 7.0 years
The Number of years it takes to accumulate $15000.0 is 8.0 years
The Number of years it takes to accumulate $15000.0 is 9.0 years
The Number of years it takes to accumulate $15000.0 is 10.0 years
The Number of years it takes to accumulate $15000.0 is 11.0 years
The Number of years it takes to accumulate $15000.0 is 12.0 years
어떻게 그냥 마지막 줄을 인쇄합니까?
@Yusuf Ning Double을 사용하지 말고 대신 BigDecimal을 사용하십시오! 이중/부동은 재무/계산 용도로 사용되지 않습니다. – Alboz
[자바 스타일 가이드에 따라 코드의 형식을 지정하고 변수의 이름을 지정하는 방법을 배우십시오.선행 중괄호가가는 곳은 종교적 전투입니다. 그 밖의 모든 것들이 자리하고 있습니다.] (https://github.com/twitter/commons/blob/master/src/java/com/twitter/common/styleguide.md)) –