gpt4 book ai didi

c - 锻炼 do while 循环

转载 作者:行者123 更新时间:2023-11-30 21:39:22 26 4
gpt4 key购买 nike

我正在 do...while 循环中工作。我需要对代码进行一些修复。

我的程序是这样的:

    #include <stdio.h>
#include <conio.h>
void main()

{

int n, score, ctr=1, total=0, ts=0;
float average=0;

printf("\n\nEnter How Many Scores: ");
scanf("%d",&n);

do{

printf("\nEnter score %d: ",ctr++);
scanf("%d",&score);
total+=score;
average=total/n;
if(score>=50&&100<=score);
printf("\nTotal Score from 50-100: %d",score);
if(score%2==0);
printf("\nTotal even number score: %d",score);
if(score%2!=0);
printf("\nTotal odd number score: %d",score);

}while(ctr<=n);
printf("\nTotal Score: %d",total);
printf("\nAverage: %.2f",average);

getch();

}

我需要找到的是50-100的总分,偶数总分和奇数总分。提前致谢。

最佳答案

如果总分、总偶数和总奇数可以使用单独的变量,那就更好了。然后在条件检查中更新相应的总变量。

int total_fh = 0, total_es = 0, total_os = 0;

if (score >=50 && score <= 100)
{
total_fh += score;

if (score % 2 == 0) {
total_es += score;
}
else if (score % 2 != 0)
{
total_os += score;
}
}

然后在循环外部,使用 printf 语句打印分数。

关于c - 锻炼 do while 循环,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34419111/

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