gpt4 book ai didi

java - 为什么这会给我一个无限循环?

转载 作者:行者123 更新时间:2023-12-02 07:32:10 26 4
gpt4 key购买 nike

我正在检查用于计算投资的代码,直到它翻倍,并且我收到了一个我似乎无法解决的无限循环。谁能弄清楚为什么这会给我一个无限循环?我自己经历过,但似乎找不到问题所在。所指的“期间”是指每年复利多少次。

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;
}

代码应要求三个“周期”,或输入数据每年复合的三个不同时间(例如每年、每月、每天等)

非常感谢!

最佳答案

而不是做

account =interest +base

你应该有

account = interest +account

关于java - 为什么这会给我一个无限循环?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12765754/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com