gpt4 book ai didi

java - 在 Java 循环中更新总计

转载 作者:行者123 更新时间:2023-11-30 08:26:31 25 4
gpt4 key购买 nike

每次循环运行时,我都无法更新总数。我一直在使用值 2、50 和 5 来检查我的工作。总数应为:3、4.5、6.75、10.125、15.1875,但我只能得到第一个总数。

非常感谢任何帮助!谢谢!!

这是我的代码:

import java.util.Scanner;

public class Population
{

public static void main(String[] args)
{
int startingOrganisms;
int dailyPopulation;
int daysMultiply;
double dailyTotal = 0;
double dailyTotal2 = 0;
double dailyTotal3 = 0;

Scanner keyboard = new Scanner(System.in);

System.out.println("Starting # of Organisms:");
startingOrganisms = keyboard.nextInt();

System.out.println("Daily Population Increase(%):");
dailyPopulation = keyboard.nextInt();

System.out.println("# of Days They'll Multiply:");
daysMultiply = keyboard.nextInt();

for (int i = 1; i<=daysMultiply; i++)
{
dailyTotal = startingOrganisms*dailyPopulation*.01;
dailyTotal2 = startingOrganisms + dailyTotal;
System.out.println("Population for day " + i + " = " + dailyTotal2);
}

}

}

最佳答案

你只能得到第一个总数,因为你只计算了一次!

你应该读取用户的输入并循环计算:

while(daysMultiply != -1){ // when the user wants to quit he'll enter -1, for example
daysMultiply = keyboard.nextInt();
dailyTotal = 0;
dailyTotal2 = 0;
for (int i = 1; i<=daysMultiply; i++)
{
dailyTotal = startingOrganisms*dailyPopulation*.01;
dailyTotal2 = startingOrganisms + dailyTotal;
System.out.println("Population for day " + i + " = " + dailyTotal2);
}
}

关于java - 在 Java 循环中更新总计,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21412809/

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