| 网站首页 | 业界新闻 | 小组 | 威客 | 人才 | 下载频道 | 博客 | 代码贴 | 在线编程 | 编程论坛
欢迎加入我们,一同切磋技术
用户名:   
 
密 码:  
共有 1087 人关注过本帖
标题:else if 嵌套使用的问题.
只看楼主 加入收藏
bluesky3810
Rank: 1
等 级:新手上路
帖 子:35
专家分:0
注 册:2007-9-17
收藏
 问题点数:0 回复次数:5 
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
缘吇弹
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:地球
等 级:版主
威 望:43
帖 子:3038
专家分:27
注 册:2007-7-2
收藏
得分:0 
以下是引用bluesky3810在2007-9-29 11:32:53的发言:

/*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位置错误?



注意红色的花括号.语句块要用{}括起来.


Repeat  Life=Study;Until (death);
2007-09-29 11:45
缘吇弹
Rank: 16Rank: 16Rank: 16Rank: 16
来 自:地球
等 级:版主
威 望:43
帖 子:3038
专家分:27
注 册:2007-7-2
收藏
得分:0 
下边的有需要的话,你自己改过来.

Repeat  Life=Study;Until (death);
2007-09-29 11:46
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
远去的列车
Rank: 1
等 级:新手上路
威 望:2
帖 子:205
专家分:0
注 册:2007-8-7
收藏
得分:0 
if (grades>=0 && grades<=100)

else if (grades<0 || grades>100)

else
{ //此处程序跑不进来,grades的情况上面两种已经全部包括了。
}

C++学习
2007-09-29 13:44
远去的列车
Rank: 1
等 级:新手上路
威 望:2
帖 子:205
专家分:0
注 册:2007-8-7
收藏
得分:0 
do
{
printf ("score %i: ",grade_counter);
scanf ("%i",&grades);
getch(); //吃掉回车
//……
}
呃,主要问题 输入非数字情况 还末解决

[此贴子已经被作者于2007-9-29 13:58:18编辑过]


C++学习
2007-09-29 13:57
快速回复:else if 嵌套使用的问题.
数据加载中...
 
   



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

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