发布网友
共3个回答
热心网友
switch主要是选择功能,判断switch后面的条件,如果符合case里面哪一个,就进行相应操作,例如如下代码。
case 1:
printf("1");
case 2:
printf("2");
break;
case 3:
printf("3");
break;
如果这样写
1 的时候 输出 12
2 的时候 输出 2
3 的时候 输出 3
case会一直向下执行,直到碰到break语句,或者switch对应的大括号,才会停止
热心网友
#include<stdio.h>
int main()
{
float w,h,t;
int n;
printf("请输入身高:");
scanf("%f",&h);
printf("请输入体重:");
scanf("%f",&w);
t=w/(h*h);
if(t<18)
n=1;
else if(t>=18&&t<25)
n=2;
else if(t>=25&&t<27)
n=3;
else if(t>=27)
n=4;
switch(n)
{
case 1:
printf("体轻型\n");
break;
case 2:
printf("正常体型\n");
break;
case 3:
printf("超重体型\n");
break;
case 4:
printf("肥胖体型\n");
break;
}
return 0;
}
非要用switch的话 就这样!!!
热心网友
这个不能用switch,只能if else
热心网友
switch主要是选择功能,判断switch后面的条件,如果符合case里面哪一个,就进行相应操作,例如如下代码。
case 1:
printf("1");
case 2:
printf("2");
break;
case 3:
printf("3");
break;
如果这样写
1 的时候 输出 12
2 的时候 输出 2
3 的时候 输出 3
case会一直向下执行,直到碰到break语句,或者switch对应的大括号,才会停止
热心网友
#include<stdio.h>
int main()
{
float w,h,t;
int n;
printf("请输入身高:");
scanf("%f",&h);
printf("请输入体重:");
scanf("%f",&w);
t=w/(h*h);
if(t<18)
n=1;
else if(t>=18&&t<25)
n=2;
else if(t>=25&&t<27)
n=3;
else if(t>=27)
n=4;
switch(n)
{
case 1:
printf("体轻型\n");
break;
case 2:
printf("正常体型\n");
break;
case 3:
printf("超重体型\n");
break;
case 4:
printf("肥胖体型\n");
break;
}
return 0;
}
非要用switch的话 就这样!!!
热心网友
这个不能用switch,只能if else