马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
本帖最后由 划句顾 于 2021-5-26 10:50 编辑
switch怎么用,请点这里C语言的switch
/*****************************************************************
@fuction:给出一个百分制成绩,要求输出成绩等级A,B,C,D,E。90分以上为A,80~89分为B,70~79分为C,60~69分为D,60分以下为E。(用switch语句)。
@time :2021/3/30
@author :LaoGu
******************************************************************/
#include<stdio.h>
int main()
{
int score,grade;
printf("Please input your score:");
scanf("%d",&score);
while(score>100||score<0)
{
printf("Your score is error!Please retry.\nYour score is:");
scanf("%d",&score);
}
grade = score / 10;
switch(grade)
{
case 0:
printf("Your grade is E\n");break;
case 1:
printf("Your grade is E\n");break;
case 2:
printf("Your grade is E\n");break;
case 3:
printf("Your grade is E\n");break;
case 4:
printf("Your grade is E\n");break;
case 5:
printf("Your grade is E\n");break;
case 6:
printf("Your grade is D\n");break;
case 7:
printf("Your grade is C\n");break;
case 8:
printf("Your grade is B\n");break;
case 9:
printf("Your grade is A\n");break;
case 10:
printf("Your grade is A\n");break;
}
return 0;
}
[b]注意:[/b]switch(expression)的expression是一个常量表达式,必须是一个整型或枚举类型。
/*****************************************************************
@fuction:给出一个百分制成绩,要求输出成绩等级A,B,C,D,E。90分以上为A,80~89分为B,70~79分为C,60~69分为D,60分以下为E。(用if-else语句)。
@time :2021/3/30
@author :LaoGu
******************************************************************/
#include<stdio.h>
int main()
{
int score,g;
printf("Please input your score:");
scanf("%d",&score);
while(score>100||score<0)
{
printf("Your score is error,please retry!\nyour score:");
scanf("%d",&score);
}
if(score>=90)
{
printf("Your grade is A!\n");
}
else if(score>=80&&score<90)
{
printf("Your grade is B!\n");
}
else if(score>=70&&score<80)
{
printf("Your grade is C!\n");
}
else if(score>=60&&score<70)
{
printf("Your grade is D!\n");
}
else
{
printf("Your grade is E!\n");
}
return 0;
}
|