下面代码的功能是将百分制成绩转换为5分制成绩
功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出Inputerror!,并允许用户重新输入,直到输入合法数据...
功能是:如果用户输入的是非法字符或者不在合理区间内的数据(例如输入的是a,或者102,或-45等),则程序输出 Input error!,并允许用户重新输入,直到输入合法数据为止,并将其转换为5分制输出。目前程序存在错误,请将其修改正确。并按照下面给出的运行示例检查程序。
#include<stdio.h>
int main()
{
int score;
char grade;
printf("Please input score:");
scanf("%d", &score);
if (score < 0 || score > 100)
printf("Input error!\n");
else if (score >= 90)
grade = 'A’;
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'E';
printf("grade:%c\n", grade);
return 0;
}
程序运行结果示例1:
Please input score:
a↙
Input error!
Please input score:
-12↙
Input error!
Please input score:
230↙
Input error!
Please input score:
92↙
grade: A
程序运行结果示例2:
Please input score:
88↙
grade: B
程序运行结果示例3:
Please input score:
73↙
grade: C
程序运行结果示例4:
Please input score:
65↙
grade: D
程序运行结果示例5:
Please input score:
27↙
grade: E
输入格式:
"%d" 展开
#include<stdio.h>
int main()
{
int score;
char grade;
printf("Please input score:");
scanf("%d", &score);
if (score < 0 || score > 100)
printf("Input error!\n");
else if (score >= 90)
grade = 'A’;
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'E';
printf("grade:%c\n", grade);
return 0;
}
程序运行结果示例1:
Please input score:
a↙
Input error!
Please input score:
-12↙
Input error!
Please input score:
230↙
Input error!
Please input score:
92↙
grade: A
程序运行结果示例2:
Please input score:
88↙
grade: B
程序运行结果示例3:
Please input score:
73↙
grade: C
程序运行结果示例4:
Please input score:
65↙
grade: D
程序运行结果示例5:
Please input score:
27↙
grade: E
输入格式:
"%d" 展开
3个回答
展开全部
#include<stdio.h>
#include<stdlib.h>
int main()
{
int score=-1;
char grade=' ';
printf("Please input score:\n");
scanf("%d", &score);
if (score < 0 || score > 100)
printf("Input error!\n");
else{
if (score >= 90)
grade = 'A';
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'E';
printf("grade: %c\n", grade);
}
system("pause");
return 0;
}
要改一下他的分支语句的结构,省的printf多次,当输入错误的时候是不用输出成绩的。
展开全部
#include<stdio.h>
int main()
{
int score=-1;
char grade;
printf("Please input score:");
scanf("%d", &score);
while(score < 0 || score > 100)
{
getchar();
printf("Input error!\n");
printf("Please input score:");
scanf("%d", &score);
}
if (score >= 90)
grade = 'A';
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'E';
printf("grade:%c\n", grade);
return 0;
}
本回答被提问者采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
展开全部
#include<stdio.h>
int main()
{
int score;
char grade;
int i;
while(1)
{
printf("Please input score:");
if ( scanf("%d", &score) == EOF ){
break;
}
while( getchar()!='\n' );
if (score < 0 || score > 100) {
printf("Input error!\n");
continue;
}
else if (score >= 90)
grade = 'A';
else if (score >= 80)
grade = 'B';
else if (score >= 70)
grade = 'C';
else if (score >= 60)
grade = 'D';
else
grade = 'E';
printf("grade:%c\n", grade);
}
return 0;
}
本回答被网友采纳
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询