如果分数高于100,则无法显示“无效分数。请重新输入”

时间:2014-12-21 10:56:52

标签: c arrays if-statement for-loop main

noob here ...

一直在尝试为群组作业编写c编程。除非计算得分高于100(例如101),否则一切都会顺利进行。我的问题是要求对分数进行分类并给出平均值,最后,如果它超出范围,则应该给出输出通知分数无效并且需要重新输入。这是我的代码:

#include<stdio.h>

main()
{
int n=0,i=0;
int scores[100],average=0, total=0;
int outstanding=0,satisfactory=0,unsatisfactory=0;

printf("Number of scores you want to enter:\n");
scanf("%d",&n);//how many times the score needed to be entered

printf("Please enter the scores:\n");

for(i=0;i<n;i++)//store user input in array
{

scanf("%d", &scores[i]);

if ((scores[i]<0) && (scores[i]>100))
{
printf("\nScores invalid. Please re-enter:\n");
i--;
}
}

for(i=0;i<n;i++)// to categorize
{

if((scores[i]>=0) && (scores[i]<=59))
{
unsatisfactory++;

}
else if((scores[i]>=60) && (scores[i]<=89))
{
satisfactory++;

}
else if((scores[i]>=90) && (scores[i]<=100))
{
outstanding++;

}
}
for (i=0;i<n;i++)
{

total = total + scores[i];
average = total / n;
}

//print out results

printf("\n The number of unsatisfactory scores is : %d\n ", unsatisfactory);
printf("\n The number of satisfactory scores is : %d\n", satisfactory);
printf("\n The number of outstanding scores is : %d\n", outstanding);
printf("\n The average of the score is :    %d\n ", average);


return 0;
}

我怀疑问题出在:

if ((scores[i]<0) && (scores[i]>100))
{
printf("\nScores invalid. Please re-enter:\n");
i--;
}

但不确定如何纠正它。即使我放了return main();,它仍然没有给出预期的输出('得分无效。请重新输入。)并返回,而是继续进行分类和平均。

帮助?樱桃在上面?

1 个答案:

答案 0 :(得分:0)

使用||而不是&amp;&amp;在你的if语句中。

if ((scores[i]<0) || (scores[i]>100))