一道C语言作业题与大家分享下哈

来源:百度知道 编辑:UC知道 时间:2024/07/09 01:28:17
就是编个程序打印出学生的成绩是否及格,良,优秀.
#include"stdio.h"
main()
{
int x,m
printf("请输入学生的成绩:\n",x);
scanf("%d",&x);
m=x/10;
switch(m)
{
case 1:case 2:case 3:case 4:case 5:cse 6:
printf("不及格");
break;
case 7:case 8:
printf("良");
break;
case 9:case 10:
printf("优秀");
break;
}
}
问题来了,我想把这个问题改进一下,我输入数字没错,但要是我输入一个字母比如"a"或不小心在数字中按到字母键比如"123a"输入时程序能自动报错:"输入有误",要怎么改?

上一楼已经弄得很好了,也运行得出来,可是你这个程序针对的成绩只是100以内的,如果输入负数或者大于100的数就不行了;
(我在上一楼的基础上做了点修改,你有时间看一下吧!)
#include<stdio.h>
int main()
{
int x,m;
printf("请输入学生的成绩:\n"); //这后面别带一个X
scanf("%d",&x);
while(getchar()!='\n'||x>100||x<0) //在这里判断一下,如果输入有误会提示重新输入
{
printf("数据输入错误,请重新输入:");
fflush(stdin);
scanf("%d",&x);
}
m=x/10;
switch(m)
{
case 9:case 10:
printf("优秀"); break;
case 7:case 8:
printf("良"); break;
case 0:case 1:case 2:case 3:case 4:case 5:case 6:
printf("不及格");
break;

}
return 0;
}

#include <conio.h>
#include <stdio.h>
#include"stdio.h"
main()
{
int x,m;
printf("请输入学生的成绩:\n",x);
scanf("%d",&x);
m=x/10;
switch(m)
{
case 1:
case