请教高手,C语言用switch语句编程,只能用switch语句,不能用if语句。问题如下:

2024年11月22日 20:31
有6个网友回答
网友(1):

请楼主查收,敬请采纳

#include "stdio.h"
#include "stdlib.h"

void main()
{
int score;
int m, n;

printf("please input score : ");
scanf("%d", &score);

n = score % 10;
m = (score - n) / 10;

switch(m)
{
case 9:
printf("A\n");
break;
case 8:
printf("B\n");
break;
case 7:
printf("C\n");
break;
case 6:
printf("D\n");
break;
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("E\n");
break;
default:
printf("ERROR\n");
}
}

网友(2):

此题很简单呀,怎么弄那么复杂呢?以下我编的程序代码:
#include
main()
{ int g;
printf("Please INput g:");scanf("%d",&g);
switch(g/10)
{ case 9:printf("A\n"); break;
case 8:prinrf("B\n");break;
case 7:printf("C\n");break;
case 6:prinf("E\n");break;
case 5:
case 4:
case 3:
case 2:
case 1:printf("\n");break;
default :printf("error");break;

}
}

网友(3):

可以用镶嵌的switch语句:
switch(a>80)
{
case 1:
swith(a<90)
{
case 1:
case 0:
}
case 0:
switch(a>70)
{
case 1:
case 0:
}
}
呵呵,思路就是这样!你自己去尝试一下吧,祝你好运……

网友(4):

switch()
{
case 99:
case 98:
case 97:
......
case 90:输出;
以下雷同
}

网友(5):

#include
#include

void main()
{
int score;
int m, n;

printf("please input score : ");
scanf("%d", &score);

m = score /10;
switch(m)
{
case 9:
printf("A\n");
break;
case 8:
printf("B\n");
break;
case 7:
printf("C\n");
break;
case 6:
case 5:
case 4:
case 3:
case 2:
case 1:
case 0:
printf("E\n");
break;
default:
printf("ERROR\n");
break;
}
}

网友(6):

运行一下程序:
#include
void main()
{
int i,k;
printf("输入成绩(0到99):");
scanf("%d",&k);
i=k/10;/*取成绩的十位数*/
switch(i) /*成绩的十位数所在的范围*/
{
case 9 :printf("A\n"); break;
case 8 :printf("B\n"); break;
case 7 :printf("C\n"); break;
case 6 :printf("D\n"); break;
case 5 :printf("D\n"); break;
case 4 :printf("D\n"); break;
case 3 :printf("D\n"); break;
case 2 :printf("D\n"); break;
case 1 :printf("D\n"); break;
case 0 :printf("D\n"); break;
default :printf("error\n");
}
}