gpt4 book ai didi

C++ 嵌套循环没有给我任何值

转载 作者:行者123 更新时间:2023-11-28 02:18:22 26 4
gpt4 key购买 nike

所以这个嵌套循环应该为每个练习生成 10 个练习的随机分数用户输入的学生。生成分数后,必须将它们全部加起来并找到平均分数。但是,当我运行该程序时,它没有给我任何值。这是代码:

int progExAvg, progExScore, studentAmt;
int counter = 0;
int progExTotal = 0;

srand(time(0));

cout << "Please enter how many students do you have in your class: ";
cin >> studentAmt;
cin.sync();

while (counter < studentAmt)
{
for(int i = 0; i < 10; i++)
{
progExScore = rand() % 100 + 1;
progExTotal = progExTotal + progExScore;
}

progExAvg = progExTotal / 10;



}
cout << progExAvg << endl;
return 0;

最佳答案

你永远不会增加counter,所以它总是小于studentAmt

while (counter < studentAmt)
{
for(int i = 0; i < 10; i++)
{
progExScore = rand() % 100 + 1;
progExTotal = progExTotal + progExScore;
}

progExAvg = progExTotal / 10;

counter++;

}

关于C++ 嵌套循环没有给我任何值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33336276/

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