您的位置:首页 > 科技 > 能源 > 东莞疫情最新消息今天中高风险区_石家庄网站设计_网站外链有多重要_常州seo招聘

东莞疫情最新消息今天中高风险区_石家庄网站设计_网站外链有多重要_常州seo招聘

2025/4/29 10:38:31 来源:https://blog.csdn.net/m0_49117509/article/details/147499554  浏览:    关键词:东莞疫情最新消息今天中高风险区_石家庄网站设计_网站外链有多重要_常州seo招聘
东莞疫情最新消息今天中高风险区_石家庄网站设计_网站外链有多重要_常州seo招聘

下面是学习的网站:

【C语言】

目录

11、if-else if-else语句

12、switch语句

13、逻辑运算符

14、scanf语句

15、终端scanf的中文乱码


11、if-else if-else语句

简单if语句:

#include <stdio.h>int main(){int a = 2;int b = 8;int result;if (a > b){result = a - b;printf("a与b的差值为:%d\n",result);}else{result = b - a;printf("a与b的差值为:%d\n",result);}return 0;
}

if嵌套语句:

#include <stdio.h>int main(){int user_type;float price;//也可以用double类型float discout;float final_price;user_type = 2;price =180;if (user_type == 1){if(price > 100){discout = 0.95;}else{discout = 1;}        }else if(user_type == 2){if(price > 200){discout = 0.9;}else{discout = 0.97;}}else{printf("该用户类型无效\n");}final_price = price * discout;printf("最终支付金额为:%.2f",final_price);return 0;
}

12、switch语句

#include <stdio.h>int main(){int commodity_type = 2;double price = 299.9;switch (commodity_type){case 1:if(price < 500){printf("电子产品价格较低,无优惠。\n");}else if(price <= 1000){printf("电子产品可享受5%%优惠。\n");}else{printf("电子产品可享受10%%优惠。\n");}break;case 2:if(price < 200){printf("服装价格较低,无优惠。\n");}else if(price <= 500){printf("服装可享受8%%优惠。\n");}else{printf("服装可享受15%%优惠。\n");}break;default:printf("商品类型既不属于电子产品又不属于服装\n");break;}
}

13、逻辑运算符

主要用于多个条件判断的情况

&& ;或 || (英文输入状态下顿号可以打出竖线);非

优先级为 () > ! > && > ||

#include <stdio.h>int main(){int year = 2088;if(year % 4 == 0 && year % 100 != 0){printf("%d年是闰年\n",year);}else if(year % 400 == 0){printf("%d年是闰年\n",year);}//UP主用了一个包含逻辑运算符的条件语句就达成了以上的代码//if((year % 4 ==0) && !(year % 100 == 0) || (year % 400 == 0))else{printf("%d年不是闰年\n",year);}
}

14、scanf语句

用来接收用户输入的内容并把数据存起来。

此时不能在VSCode的问题窗口进行输入,而是要在终端进行。需要在设置里面设置在终端(Terminal)运行代码(Run Code)。

scanf语句和printf语句的格式比较类似,尤其要注意变量地址前面有一个取址符&

scanf("格式字符串",&变量地址1,&变量地址2...); 

以下是scanf的练习:

#include <stdio.h>int main(){char category;float price;printf("请输入商品类型,A代表电子产品,B代表服装:\n");scanf(" %c",&category);printf("请输入商品价格:\n");scanf(" %f",&price);if(category == 'A'){if(price < 500){printf("电子产品价格较低,无优惠\n");}else if (price >= 500 && price < 1000){printf("电子产品可享受5%%的优惠\n");}else{printf("电子产品可享受10%%的优惠\n");}}else if(category == 'B'){if(price < 200){printf("服装价格较低,无优惠\n");}else if (price >= 200 && price < 500){printf("服装可享受8%%的优惠\n");}else{printf("服装可享受15%%的优惠\n");}}else{printf("此商品既不是电子产品也不是服装\n");}//    使用switch  //    char commodity_type;//    double price;//    printf("请输入商品类型,A代表电子产品,B代表服装:\n");//    scanf("%c",&commodity_type);//    printf("请输入商品价格:\n");//    scanf(" %f",&price);//    switch (commodity_type)//    {//    case 'A'://        if(price < 500){//            printf("电子产品价格较低,无优惠。\n");//        }//        else if(price <= 1000){//            printf("电子产品可享受5%%优惠。\n");//        }//        else{//            printf("电子产品可享受10%%优惠。\n");//        }//        break;//    case 'B'://        if(price < 200){//        printf("服装价格较低,无优惠。\n");//        }//        else if(price <= 500){//        printf("服装可享受8%%优惠。\n");//        }//        else{//        printf("服装可享受15%%优惠。\n");//        }//        break;//    default://        printf("商品类型既不属于电子产品又不属于服装\n");//        break;//    } 
}

15、终端scanf的中文乱码

从“C语言scanf | 你的角色名是啥?”这一章节后就有许多人在评论区说终端有乱码问题,我的解决办法是在VSCode的setting.json文件中进行相关设置:

{// 基础界面设置"workbench.colorTheme": "Default Dark Modern","files.autoSave": "onFocusChange","explorer.confirmDelete": false,"workbench.statusBar.visible": true,"files.encoding":"utf8",// C/C++ 开发环境配置"C_Cpp.default.includePath": ["F:\\msys2\\mingw64\\include","F:\\msys2\\mingw64\\lib\\gcc\\x86_64-w64-mingw32\\14.2.0\\include"],// 中文编码相关设置 - 解决中文乱码的核心配置//"files.encoding": "gb2312","files.autoGuessEncoding": true,"[c]": {//"files.encoding": "gb2312""files.encoding":"utf8"},"[cpp]": {//"files.encoding": "gb2312""files.encoding":"utf8"},// 终端设置 - 解决控制台输出中文乱码"terminal.integrated.defaultProfile.windows": "Command Prompt","terminal.integrated.profiles.windows": {"Command Prompt": {"path": "C:\\Windows\\System32\\cmd.exe","args": []}},"terminal.integrated.shellArgs.windows": ["/k","chcp 936"],// 代码运行设置 - 解决程序运行时的中文乱码"code-runner.runInTerminal": true,"code-runner.executorMap": {"c": "cd $dir && gcc $fileName -o $fileNameWithoutExt -fexec-charset=GBK && $dir$fileNameWithoutExt"},"code-runner.saveFileBeforeRun": true,"code-runner.clearPreviousOutput": true,

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com