我正在查看一个用于计算投资的代码,直到它翻了一番,我收到了一个我似乎无法解决的无限循环。谁能弄清楚为什么这给了我一个无限循环?我已经经历了自己,但我似乎无法找到问题所在。所指的“期间”是每年复利的次数。
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;
}
代码应要求三个“期间”,或输入数据每年复合的三个不同时间(例如每年、每月、每天等)
非常感谢!