gpt4 book ai didi

java - 在 For 循环中使用计数器?

转载 作者:行者123 更新时间:2023-11-29 08:40:47 30 4
gpt4 key购买 nike

如何在此 for 循环中正确部署 gradecount 计数器?

它应该在每个等级输入后递增。

for (int i = 0; i < numOfGrades; i++){
double belowzero = gradeinput.nextDouble();
int gradecount = 1;
System.out.println("enter grade number " + gradecount + ":" );
gradecount++;

while (belowzero < 0){
belowzero = gradeinput.nextDouble();
}
grades[i] = belowzero;
}
}

最佳答案

在 for 循环外初始化 gradecount。

int gradecount = 1;
for (int i = 0; i < numOfGrades; i++){
double belowzero = gradeinput.nextDouble();

System.out.println("enter grade number " + gradecount + ":" );
gradecount++;

while (belowzero < 0){
belowzero = gradeinput.nextDouble();
}
grades[i] = belowzero;
}
}

关于java - 在 For 循环中使用计数器?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40415036/

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