我在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,总偶数得分和总奇数得分。提前谢谢。
答案 0 :(得分:0)
你有if(score>=50&&100<=score);
如果您希望得分在50到100之间,则需要将其更改为if(score>=50 && score<=100)
。
你也需要在if语句之后删除分号。
此外,如果要计算偶数和奇数分数的总数,则需要跟踪每个分数的单独变量。像这样:
#include <stdio.h>
#include <conio.h>
int main(void)
{
int n, score, ctr=1, total=0, ts=0;
float average=0;
int odds=0;
int evens=0;
printf("\n\nEnter How Many Scores: ");
scanf("%d",&n);
do{
printf("\nEnter score %d: ",ctr++);
scanf("%d",&score);
total+=score;
if(score>=50 && score<=100 )
printf("\nTotal Score from 50-100: %d",score);
if(score%2==0)
{
evens++;
printf("\nTotal even score: %d",evens);
}
if(score%2!=0)
{
odds++;
printf("\nTotal odd score: %d",odds);
}
}while(ctr<=n);
printf("\nTotal Score: %d",total);
if((odds+evens) !=0) average= (float) total/(odds+evens);
printf("\nAverage: %.2f", average );
getch();
}
答案 1 :(得分:0)
如果你可以使用单独的变量来获得总分,总分数和总分数,那就更好了。然后在条件检查中更新相应的总变量。
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
语句打印分数。