두 배가 될 때까지 투자를 계산하는 데 사용 된 코드를 통해 해결할 수없는 무한 루프가 발생했습니다. 왜 이것이 내게 무한 루프를주고 있는지 알아낼 수 있습니까? 나는 나 자신을 지났지 만 문제를 찾을 수없는 것 같습니다. 언급 된 "기간"은 일년에 몇 번이자가 복리가 되는가입니다.왜 이렇게하면 무한 루프가 생깁니 까?
double account = 0; //declares the variables to be used
double base = 0;
double interest = 0;
double rate = 0;
double result = 0;
double times = 0;
int years = 0;
int j;
System.out.println("This is a program that calculates interest.");
Scanner kbReader = new Scanner(System.in); //enters in all data
System.out.print("Enter account balance: ");
account = kbReader.nextDouble();
System.out.print("Enter interest rate (as decimal): ");
rate = kbReader.nextDouble();
System.out.println(" " + "Years to double" + " " + "Ending balance");
base = account;
result = account;
for (j=0; j<3; j++){
System.out.print("Enter period: ");
times = kbReader.nextDouble();
while (account < base*2){
interest = account * rate/times;
account = interest + base;
years++;
}
account = (((int)(account * 100))/100.0);
//results
System.out.print(" " + i + " " + account + "\n");
account = result;
}
코드는 세 가지 "기간"을 요청해야한다, 또는 세 개의 서로 다른 시간에 입력 한 데이터는 (매년 전을, 매월, 매일 등) 연간 복잡해집니다
고마워요!
당신이 코드에 중단 점을 설정하고 그것을 통해 스테핑이 디버깅을 시도해야합니까? – Bohemian
아니요, 제가 사용하고있는 IDE가 비교적 새로운 BlueJ입니다. 의견 있으십니까? – ParaChase
'int'에 대한 캐스트가'account'를 base의 값 아래로 떨어 뜨리지 않는다고 보장 할 수는 없습니다. 'base * 2> = Integer.MAX_INTEGER'이면. 또한'times'이 0이기 때문에'(int)'형 변환은 항상'account'에'Integer.MAX_INTEGER'를 남겨 둘 것이라고 믿습니다. –