gpt4 book ai didi

多个项目的 Java 循环

转载 作者:太空宇宙 更新时间:2023-11-04 11:22:16 25 4
gpt4 key购买 nike

我正在java中工作,我希望只要有用户指定的项目,我的循环就可以运行。截至目前,代码运行了一次迭代,并读取了一次循环的结果,但不会再继续下去。

如果用户说有 3 个项目,我希望代码运行循环 3 次并告诉我每个项目的总数。现在,无论我指定项目数量是 1 还是 5,它都会告诉我 1 个项目的总数。

    double projectBoardFootage = 1.0;
double projectBoardFootageTotal = 0.0;
int i = 0;

System.out.println("How many projects do you want to estimate?");
int numberOfProjects = scan.nextInt();


for(i = 0; i < numberOfProjects; ++i) {
while (projectBoardFootage > 0) {
System.out.println("Enter your board footage for Project #" + (i + 1) + " (0 to exit)");
projectBoardFootage = scan.nextDouble();
projectBoardFootageTotal += projectBoardFootage;
}
System.out.println("The raw board footage for Project #" + (i + 1) + " is: " + projectBoardFootageTotal);
}

最佳答案

也许您想重新初始化总板素材以清除之前的迭代计数?

for(i = 0; i < numberOfProjects; ++i) {
projectBoardFootageTotal = 0.0;
while (projectBoardFootage > 0) {
System.out.println("Enter your board footage for Project #" + (i + 1) + " (0 to exit)");
projectBoardFootage = scan.nextDouble();
projectBoardFootageTotal += projectBoardFootage;
}
System.out.println("The raw board footage for Project #" + (i + 1) + " is: " + projectBoardFootageTotal);
}

关于多个项目的 Java 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44769938/

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