2024-07-10 00:04来源:未知作者:admin
#include <stdio.h>
int main(void)
{
int n;
int flag;
printf(请输入一个整数:);
scanf(%d,&n);
flag = n % 2;
switch(flag)
{
case 0: printf(%d是偶数\n,n);break;
case 1: printf(%d是奇数\n,n);break;
default:break;
}
}
double是可以隐形转为int 。但是switch括号中表达式必须返回一个整型,而你这里(r/10)是将10转为double,返回的是double ,所以显示错误。假如你想转为int 。可以和你上面一样进行一个强制转换,或者定义一个做操作数 int i; 然后switch(i=r/10) 这样运行表达式的时候左操作数占主导地位,返回结果是int 。
试试这个
public class Bmi {
public static void main(String[] args){
float bmi;
bmi=17.6;
switch ((bmi-16)>0){
case false:
system.out.print(严重偏轻);
break;
case true:
switch ((bmi-18)>0){
case false:
system.out.print(偏轻);
break;
case true:
switch ((bmi-24)>0){
case false:
system.out.print(正常体重);
break;
case true:
switch ((bmi-29)>0){
case false:
system.out.print(超重);
break;
case true:
switch ((bmi-35)>0){
case false:
system.out.print(严重超重);
break;
case true:
system.out.print(非常严重超重);
break;
}
break;
}
break;
}
break;
}
break;
}
}
}
楼主你好~
switch内部的类型没有具体要求,不过一般来说只有基本类型能被接受,因为这个类型必须能和下面case标签的值进行比较。所以大约有下面这些类型:
sbyte、byte、short、ushort、int、uint、long、ulong、char、string。
以及包括可以隐形转化为这些类型的类型。
另外,其中还可以接受枚举型。
请追问~
1.选择结构if语句格式及其来使用自 A:if语句的格式: if(比较表达式21131) { 语句体52611; }else if(比较表达式2) { 语句体2; }else if(比较表达式3) { 语句体3; } ... else { 语句体n+1; } B:执行流程: 首先计算4102比较表达式1看其返回值是1653true还是false, 如...
switch(i)的语法很特殊.每一个case后的值和switch(i)中的i值比,相同就执行这个分支.但执行每个分支结束时,一般都加上break;这样就跳出了switch语句.但你要把多个case分支共用一组语句这样就可以把break去掉.像楼上所编的那样...
看看这个:
#include<stdio.h>
void main()
{
switch(0)
{
case 0: printf(i=0\n);
case 1: printf(i=1\n);break;
case 2: printf(i=2\n);
//default: printf(other\n);
}
switch(1)
{
case 0: printf(i=0\n);
case 1: printf(i=1\n);break;
case 2: printf(i=2\n);
//default: printf(other\n);
}
}