2019-03-13 C语言学习17 -switch语句实现多分支选择

1.先告诉等级,再显示成绩

设计思路:

注意;break使用。

代码:

#include

int main()

{

char grade;

scanf("%c",&grade);

printf("Your grade:");

switch(grade)

{

case 'A' :printf("85~100\n");break;

case 'B' :printf("70~84\n");break;

case 'C' :printf("60~69\n");break;

case 'D' :printf("<60\n");break;

default: printf("enter error data!\n");

}

return 0;

}

结果展示:


2.先告诉成绩,再显示等级


先告诉成绩,显示等级,需要在switch的表达式里进行计算-写出判断表达式,为了缩到1,2,3,则强制转换((int)(score/10))

代码:

#include

int main()

{

  float score;

  char grade;

  printf("请输入学生的成绩:");

  scanf("%f",&score);

while(score>100||score<0)

{

printf("\n输入有误,请重输");

scanf("%f",&score);

}

switch((int)(score/10))

{

case 10: 

case 9:grade='A';break;

case 8:grade='B';break;

case 7:grade='C';break;

case 6:grade='D';break;

case 5:

case 4:

case 3:

case 2:

case 1:

case 0:grade='E';

}

printf("成绩是%5.lf%,相应的等级是%c\n",score,grade);

  return 0;

}

结果展示:



3.

代码

#include

int main()

{

int num,indiv,ten,hundred,thousand,ten_thousand,place;

printf("请输入一个整数(0-99999):");

scanf("%d",&num);

if(num>9999)

place = 5;

else if(num>999)

place=4;

else if(num>99)

place=3;

else if(num>9)

place=2;

else place=1;

printf("位数:%d\n",place);

printf("每位数字为:");

ten_thousand=num/10000;

thousand=(int)(num-ten_thousand*10000)/1000;

hundred=(int)(num-ten_thousand*10000-thousand*1000)/100;

ten=(int)(num-ten_thousand*10000-thousand*1000-hundred*100)/10;

indiv=(int)(num-ten_thousand*10000-thousand*1000-hundred*100-ten*10);

switch(place)

{

case 5:printf("%d,%d,%d,%d,%d",ten_thousand,thousand,hundred,ten,indiv);

printf("\n反序数字为:");

printf("%d%d%d%d%d\n",indiv,ten,hundred,thousand,ten_thousand);

break;

case 4:printf("%d,%d,%d,%d",thousand,hundred,ten,indiv);

printf("\n反序数字为:");

printf("%d%d%d%d\n",indiv,ten,hundred,thousand);

break;

case 3:printf("%d,%d,%d",hundred,ten,indiv);

printf("\n反序数字为:");

printf("%d%d%d\n",indiv,ten,hundred);

break;

case 2:printf("%d,%d",ten,indiv);

printf("\n反序数字为:");

printf("%d%d\n",indiv,ten);

    break;

case 1:printf("%d",indiv);

printf("\n反序数字为:");

printf("%d\n",indiv);

break;

}

  return 0;

}

结果展示:


4.






5.

6.


你可能感兴趣的:(2019-03-13 C语言学习17 -switch语句实现多分支选择)