
void main()
{
int age
printf("please input the child age:")
scanf("%d",&age)
switch(age)
{
case 2:
case 3: printf("%d,enter the Lower class.\n",age)break
case 4: printf("%d,enter the Middle class.\n",age)break
case 5:
case 6: printf("%d,enter the Higher class.\n",age)break
default : printf(" you chlid shuld goto more Higher class.\n")break
}
}
#include"math.h"main()
{
float p,d,f
printf("please inout consumption\n")
scanf("%f",&p)
if(p<100)
{m==0
f=p}
else if(p>=100&&p<200)
{m==o.o5
f=(1-0.05)*p}
else if(p>=200&&p<500)
{m==0.1
f=(1-0.1)*p}
else if(p>=500&&p<1000)
{m==0.15
f=(1-0.15)*p}
else(p>=1000)
{m==o.2
f=(1-0.2)*p}
printf("discount is %f,amount is %f",m,f)
}
试试看!
执行过程:先计算并获得switch后面小括号里的表达式或变量值,然后将计算结果顺序与每个case后的常量比较。
当二者相等时,执行个case块中的代码,当遇到break时,就跳出switch选择结构,执行switch选择结构之后的代码。
如果任何一个case之后的常量与switch后的小括号中的值相等,则执行switch尾部的default块中代码。
欢迎分享,转载请注明来源:内存溢出
微信扫一扫
支付宝扫一扫
评论列表(0条)