标题:else if 嵌套使用的问题.
取消只看楼主
bluesky3810
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-9-17
 问题点数:0 回复次数:1 
else if 嵌套使用的问题.

/*program to calculate the average of a set of grades and count the number of failing test grades */
#include <stdio.h>
main (void)
{
int grades,sum=0,grade_counter=1,failures=0,excellent=0;
float average;
char end_flag;
printf ("please enter the scores one by one!\n");

do
{
printf ("score %i: ",grade_counter);
scanf ("%i",&grades);
if (grades>=0 && grades<=100)
printf ("%i\n",grades);
sum=sum+grades;
grade_counter++;
if (grades<75)
failures++;
else
excellent++;

else if (grades<0 || grades>100)
printf ("sorry,what you've just entered exceed the right range of score,please enter a new score between 0~100!");
else
printf ("you've just entered a non-number,do you want to end entering? (Y/N)");
scanf ("%c",&end_flag);
{
if (end_flag=='y')
break;
else
printf ("please continue,just type in another data,make sure what you entered is a number between 0~100");
}
}
while (end_flag=='y');
average=(float) sum/grade_counter;
printf("The average score is: %f\n\n",average);
printf("and there are %i failures.",failures);
getch();
return 0;
}

这个程序错在哪儿了?为什么会编译失败,并一直提示红色标记处的else位置错误?

[此贴子已经被作者于2007-9-29 11:34:56编辑过]

搜索更多相关主题的帖子: 嵌套 else grades average 
2007-09-29 11:32
bluesky3810
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-9-17
得分:0 

错误我已经改过来了,输入数字时运行结果正常,但输入字母时,

运行结果似乎没有按照设想的程序走,请帮忙给查查错在哪儿了,我找了好久了都没找出来.

改过后的程序:


/*program to calculate the average of a set of grades and count the number of failing test grades */
#include <stdio.h>
main (void)
{
int grades,sum=0,grade_counter=1,failures=0,excellent=0;
float average;
char end_flag;
printf ("please enter the scores one by one!\n");

do
{
printf ("score %i: ",grade_counter);
scanf ("%i",&grades);
if (grades>=0 && grades<=100)
{
printf ("%i\n",grades);
sum=sum+grades;
grade_counter++;
if (grades<75)
failures++;
else
excellent++;
}

else if (grades<0 || grades>100)
printf ("\nsorry,what you've just entered exceed the right range of score,please enter a new score between 0~100!\n\n");
else
{
printf ("you've just entered a non-number,do you want to end entering? (Y/N)");
scanf ("%c",&end_flag);
{
if (end_flag=='y')
break;
else
printf ("please continue,just type in another data,make sure what you entered is a number between 0~100");
}
}
}
while (end_flag != 'y');
average=(float) sum/grade_counter;
printf("The average score is: %f\n\n",average);
printf("and there are %i failures.",failures);
getch();
return 0;
}


输入数字时运行结果:

[IMG]http://fm191.img.xiaonei.com/tribe/20070929/13/18/A310574565429HEI.jpg[/IMG]

输入字母时运行结果:
[IMG]http://fm191.img.xiaonei.com/tribe/20070929/13/24/A346105265252EWE.jpg[/IMG]

为什么输入字母时没有按设想的提示"please continue,just type in another data,make sure what you entered is a number between 0~100",而是提示"sorry,what you've just entered exceed the right range of score,please enter a new score between 0~100!",该如何解决这个问题,达到设想的实现交互的目的?
2007-09-29 13:28



参与讨论请移步原网站贴子:https://bbs.bccn.net/thread-173956-1-1.html




关于我们 | 广告合作 | 编程中国 | 清除Cookies | TOP | 手机版

编程中国 版权所有,并保留所有权利。
Powered by Discuz, Processed in 0.057104 second(s), 8 queries.
Copyright©2004-2024, BCCN.NET, All Rights Reserved