3条回答 默认 最新
- bingbingyihao 2021-09-25 19:12关注
回答:很容易理解,代码如下(它的空行不一定都一样,行数可能会有变化):
#include<stdio.h> int main() { int m; printf("Input m:"); scanf("%d",&m); if(m>0) { if(m%2==0) { printf("%d is a positive even\n",m); //是正偶数 } else { printf("%d is a positive odd\n",m); //是正奇数 } } else if(m<0) { if(m%2==0) { printf("%d is a negative even\n",m); //是负偶数 } else { printf("%d is a negative odd\n",m); //是负奇数 } } else { printf("%d is a zero .It is an even\n",m); } return 0; }
这里用的是 else if,其实用if,会导致出现判断出错,因为else和它最近的if相连,没有与之匹配的,会出错,加入else if语句,是为了看起来好看和表达起来容易理解,下面这个代码也可能完成一样的功能,分成三种情况,不符合if 条件就直接过,三个里面只会有一个符合
#include<stdio.h> int main() { int m; printf("Input m:"); scanf("%d",&m); if(m>0) { if(m%2==0) { printf("%d is a positive even\n",m); //是正偶数 } else { printf("%d is a positive odd\n",m); //是正奇数 } } if(m<0) { if(m%2==0) { printf("%d is a negative even\n",m); //是负偶数 } else { printf("%d is a negative odd\n",m); //是负奇数 } } if(m==0) { printf("%d is a zero .It is an even\n",m); } return 0; }
本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报
悬赏问题
- ¥15 求视频摘要youtube和ovp数据集
- ¥15 怎么改成输入一个要删除的数后现实剩余的数再输入一个删除的数再现实剩余的数用yes表示继续no结束程序
- ¥15 在启动roslaunch时出现如下问题
- ¥15 汇编语言实现加减法计算器的功能
- ¥20 关于多单片机模块化的一些问题
- ¥30 seata使用出现报错,其他服务找不到seata
- ¥35 引用csv数据文件(4列1800行),通过高斯-赛德尔法拟合曲线,在选取(每五十点取1点)数据,求该数据点的曲率中心。
- ¥20 程序只发送0X01,串口助手显示不正确,配置看了没有问题115200-8-1-no,如何解决?
- ¥15 Google speech command 数据集获取
- ¥15 vue3+element-plus页面崩溃