C#if条件判断+例子
·
第一段:条件流程控制整体说明 + 单 if 条件
//条件流程控制 满足一定的条件再去执行,不满足了 不会去执行对应代码。
// 1单条件流程
// if(){}
// () 需要写一个条件,例如大于 小于 等于 不等 是否为null等条件
//{ } 代码块
if (true) // true条件成了
{
Console.WriteLine("看看代码是否执行");
}
if (false)// false 不成立
{
Console.WriteLine("代码不执行"); // 检测无法执行的代码
}
第二段:if 单条件 年龄示例
// 用户输入一个年龄 判断年龄是否超过18岁,如果超过打印成年了
int age = int.Parse(Console.ReadLine());
if (age >18)
{
Console.WriteLine("成年了 想咋耍就咋耍");
}
第三段:if...else
// 2只有俩中情况的流程控制
// if(){}...else{}
int score = 60;
//if 如果条件成立 执行{}
if (score>=90)
{
Console.WriteLine("A");
}
else //其余的 和if条件的相反score<90
{
Console.WriteLine("A-");
}
第四段:if...else if...else 多条件判断
//3 多条件流程控制
// if(){}....else if(){}....else if(){}....else{}
int age1 = 12;
if (age1>=0 && age1<=14) // age1在0-14之间
{
Console.WriteLine("总角");
}else if (age1>14 && age1<=24)
{
Console.WriteLine("弱冠-豆蔻");
}
else if (age1>24 && age1<=40)
{
Console.WriteLine("三十而立");
}
else if (age1>40 && age1<=70)
{
Console.WriteLine("知命之年");
}else
{
Console.WriteLine("耄耋");
}
第五段:多个独立 if 判断
//4 多条件流程控制
// if...if... 每个if都需要进行判断
int count = 90;
if (count>=90)
{
Console.WriteLine("count大于等于90");
count = 60;
}
if (count<70)
{
Console.WriteLine("count小于70");
}
第六段:if 条件嵌套
//5 条件嵌套
//先判断是不是男生,再判断是年龄是否超过18,
// 如果不是男生 对应的提示
int age2 = 10;
string sex = "女";
if (sex=="男") // 满足是男生
{
if (age2>=18)
{
Console.WriteLine("是男生并且年龄超过18,可以结婚");
}
else //是男生 但是年龄不超过18
{
Console.WriteLine("不到18不允许结婚");
}
}else //不是男生
{
Console.WriteLine("不是男生");
}
——————分层线例子————————
第一段:区域注释说明
//#regin 和 #endregion 表示一块区域
第二段:判断数字奇偶
#region 1判断数字是不是偶数
int num = 9;
if (num%2==0) // 余数为0 证明是偶数
{
Console.WriteLine("是偶数");
}else
{
Console.WriteLine("是奇数");
}
#endregion
第三段:判断是否可能是闰年(简单版)
#region 2要求用户输入一个年份,如果该年份是4的倍数,则输出“可能是闰年”,否则输出“不是闰年”。(不考虑能被 400整除)
int year = int.Parse(Console.ReadLine());
if (year%4==0)
{
Console.WriteLine("可能是闰年");
}
else
{
Console.WriteLine("不是闰年");
}
#endregion
第四段:完整判断闰年
#region 3 判断年是不是闰年
int year1 = 1900;
//(year1%4==0 && year1%100!=0) 满足次条件
// year1%400==0 满足这个条件
// 满足其一就是闰年
if ((year1%4==0 && year1%100!=0)|| year1%400==0)
{
//$:进行拼接 只需要写一个字符串,中间有需要显示变量的,把变量使用{}即可
Console.WriteLine($"{year1}年是闰年");
}
else
{
Console.WriteLine($"{year1}年是平年");
}
#endregion
第五段:停车计费系统
#region 4 要求用户输入车辆类型(轿车、卡车、摩托车)和停车时长(小时),根据车辆类型和停车时长计算停车费用:
//轿车每小时5元;
//卡车每小时10元;摩托车每小时3元;停车不足1小时按1小时计费。
Console.WriteLine("请输入车辆的类型");
string type = Console.ReadLine(); //类型
Console.WriteLine("请输入停车时长");
double hour = double.Parse(Console.ReadLine()); //时长
double sum = 0;// 总价格
if (hour<1) //小时小于1
{
hour = 1; //hour 安按照1小时计算
}
/*
if (type=="轿车")
{
sum = 5 * hour;
}
else if (type=="卡车")
{
sum = 10 * hour;
}
else
{
sum = 3 * hour;
}
*/
//把上面注释改成三目运算
//判断type=="轿车" 如果等于了 sum = 单价 * 5
//type == "卡车" ? 10 * hour 满足是卡车的时候
//3 * hour; 不满足卡车 和轿车时候
sum = type == "轿车" ? 5 * hour :
type == "卡车" ? 10 * hour : 3 * hour;
Console.WriteLine("总费用为:"+sum);
#endregion
六:要求用户输入两个整数和运算符(+、-、*、/),根据运算符输出计算结果
Console.WriteLine("请输入一个整数");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入一个整数");
int num2 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入运算符");
string type = Console.ReadLine();
int sum;
if (type == "+")
{
sum = num1 + num2;
Console.WriteLine("计算结果:"+sum);
}
else if (type == "-")
{
sum = num1 - num2;
Console.WriteLine("计算结果:" + sum);
}
else if (type == "*")
{
sum = num1 * num2;
Console.WriteLine("计算结果:" + sum);
}
else
{
sum = num1 + num2;
Console.WriteLine("计算结果:" + sum);
}
再加十二道例子if语句
//判断一个数字,如果是偶数,就输出“是偶数”,不是偶数就输出“是奇数
int num = 10;
if (num % 2 == 0)
{
Console.WriteLine("是偶数");
}
else
{
Console.WriteLine("是基数");
}
//要求用户输入两个整数和运算符(+、-、*、/),根据运算符输出计算结果
Console.WriteLine("请输入一个整数");
int num1 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入一个整数");
int num2 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入运算符");
string type = Console.ReadLine();
int sum;
if (type == "+")
{
sum = num1 + num2;
Console.WriteLine("计算结果:" + sum);
}
else if (type == "-")
{
sum = num1 - num2;
Console.WriteLine("计算结果:" + sum);
}
else if (type == "*")
{
sum = num1 * num2;
Console.WriteLine("计算结果:" + sum);
}
else
{
sum = num1 + num2;
Console.WriteLine("计算结果:" + sum);
}
//1要求用户输入每天摄入的卡路里量,根据摄入量给出健康建议:
//摄入量小于1500卡路里,输出“可能摄入不足”;
//摄入量在1500到2500卡路里之间,输出“合理摄入”;
//摄入量大于2500卡路里,输出“可能摄入过多”。
Console.WriteLine("请输入卡路里");
int k = int.Parse(Console.ReadLine());
if (k <= 1500)
{
Console.WriteLine("可能摄入量不足");
}
if (k >= 1500 && k <= 2500)
{
Console.WriteLine("可能摄入量不足");
}
else
{
Console.WriteLine("设入量过多");
}
//2如果明天是周末,就去玩,否则就去上班;
//如果明天是周末,而且天气晴朗,就去室外玩;
//如果明天是周末,但是天气不晴朗,就在室内玩
string day = Console.ReadLine();
string tianqi = Console.ReadLine();
if (day == "zhoumo")
{
Console.WriteLine("出去玩");
if (tianqi == "天气晴朗")
{
Console.WriteLine("室内玩");
}
else
{
Console.WriteLine("室外玩");
}
}
else
{
Console.WriteLine("去上班");
}
//3小明参加数学考试,他和父亲达成承诺: 如果: 成绩为100分时,奖励一辆BMW;
//成绩为(80,99]时,奖励一台iphone xs max; 当成绩为[60, 80]时,奖励一个 iPad;
//其它时,什么奖励也没有。
Console.WriteLine("请输入成绩");
int score = int.Parse(Console.ReadLine());
if (score == 100)
{
Console.WriteLine("奖励一台BMW");
}
else if (score > 80 && score <= 99)
{
Console.WriteLine("奖励一台iphone 17 max");
}
if (score >= 60 && score <= 80)
{
Console.WriteLine("奖励一台IPAD");
}
else
{
Console.WriteLine("啥也没");
}
//4 大家都知道,男大当婚,女大当嫁。那么女方家长要嫁女儿,当然要提出 一定的条件:高:180cm以上;富:财
//富1千万以上;帅:是。
//如果这三个条件同时满足,则:“我一定要嫁给他!!!”
//如果三个条件有为真的情况,则:“嫁吧,比上不足,比下有余。”
//如果三个条件都不满足,则:“不嫁!
Console.WriteLine("请输入身高:");
int tall = int.Parse(Console.ReadLine());
Console.WriteLine("是否千万富翁(是/否):");
string fu = Console.ReadLine();
Console.WriteLine("是否帅气(是/否):");
string waimao = Console.ReadLine();
bool gao = tall >= 180;
bool fuQian = fu == "是";
bool shuai = waimao == "是";
if (gao && fuQian && shuai)
{
Console.WriteLine("我一定要嫁给他!!!");
}
else if (gao || fuQian || shuai)
{
Console.WriteLine("嫁吧,比上不足,比下有余。");
}
else
{
Console.WriteLine("不嫁");
}
//5输入三个边长判断是否可以组成三角形
Console.WriteLine("请输入第一条边");
int bian1 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入第二条边");
int bian2 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入第三条边");
int bian3 = int.Parse(Console.ReadLine());
if (bian1 + bian2 > bian3 && bian1 + bian3 > bian2 && bian2 + bian3 > bian1)
{
Console.WriteLine("可以组成三角型");
}
else
{
Console.WriteLine("不可以");
}
//6有台自动售货机,共6个商品。用户可以输 ? 1 - 6选择要购买的商品。
//1 矿泉 2红茶 3绿茶 4雪碧 5可口可乐 6脉动
int xuhao = int.Parse(Console.ReadLine());
switch (xuhao)
{
case 1: //day==1
Console.WriteLine("矿泉");
break;
case 2: // day==2
Console.WriteLine("红茶");
break;
case 3:
Console.WriteLine("绿茶");
break;
case 4:
Console.WriteLine("雪碧");
break;
case 5:
Console.WriteLine("可口可乐");
break;
case 6:
Console.WriteLine("脉动");
break;
default: //
Console.WriteLine("输入有误");
break;
}
//7要求用户输入身高(米)和体重(千克),根据BMI(Body Mass Index)评估健康状况:
//BMI = 体重 / (身高 ^ 2)
//BMI < 18.5 输出“偏瘦”;
//18.5 ≤ BMI < 24.9 输出“正常”;
//25 ≤ BMI < 29.9 输出“超重”;
//BMI ≥ 30 输出“肥胖
Console.WriteLine("请输入身高:");
int tall1 = int.Parse(Console.ReadLine());
Console.WriteLine("请输入体重:");
int weight = int.Parse(Console.ReadLine());
double BIM;
BIM = weight / (tall1 * tall1);
if (BIM < 18.5)
{
Console.WriteLine("偏瘦");
}
else if (BIM >= 18.5 && BIM <= 24.9)
{
Console.WriteLine("正常");
}
else if (BIM >= 25 && BIM <= 29.9)
{
Console.WriteLine("超重");
}
else
{
Console.WriteLine("肥胖");
}
//8出租价格计算方式 价格 起步价是8元 2公里是起步价 超过2公里的 每公里1.5元 超过12公里每公里2.2元 路程是35 问价格多少
double lucheng = 35;
double price = 8;
if(lucheng <= 2)
{
price = 8;
}
else if(lucheng >2 && lucheng <= 12)
{
price = 8+(12 - 2) * 1.5;
}
else if (lucheng > 12)
{
price = 8+10*1.5+(35-12) * 2.2;
}
Console.WriteLine("总价为:"+ price);
//9 输入一个数判断是不是水仙花数
//例如 151 = 1的3次方 + 5的3次方 + 1的3次方 = 151
Console.Write("请输入一个三位数:");
int num9 = int.Parse(Console.ReadLine());
//拆分百位、十位、个位
int bai = num9 / 100; //百位
int shi = num9 / 10 % 10; //十位
int ge = num9 % 10; //个位
if (bai * bai * bai + shi * shi * shi + ge * ge * ge == num9)
{
Console.WriteLine(num9 + " 是水仙花数");
}
else
{
Console.WriteLine(num9 + " 不是水仙花数");
}
//第二种方法
//9 输入一个数判断是不是水仙花数
// 例如 151 = 1的3次方 + 5的3次方 + 1的3次方 = 151
int aa =153;
int ge = aa % 10;//个位
int shi = aa % 100 / 10;
int bai = aa / 100;
Console.WriteLine(ge + ":"+shi+":"+bai);
//Math.PI// π
// Math.Pow(ge, 3);// ge的多少次方,ge的3次方 ge*ge*ge
if (aa>=100&&aa<=999)
{
if (Math.Pow(ge, 3) + Math.Pow(shi, 3) + Math.Pow(bai, 3) == aa)
{
Console.WriteLine("水仙花数");
}
else
{
Console.WriteLine("不是水仙花数");
}
}
else
{
Console.WriteLine("输入数字有误");
}
//10 自己上网搜搜五险一金怎么算 以及给你15000工资 应该交多少税? 郑州交五险一金的水平,一个月交多少税?
int salary = 15000;
//五险一金个人扣除
double yanglao = salary * 0.08; // 养老
double yiliao = salary * 0.02; // 医疗
double shiye = salary * 0.003; // 失业
double gongjijin = salary * 0.12; // 公积金
double five_one = yanglao + yiliao + shiye + gongjijin;
Console.WriteLine("五险一金个人每月扣除:" + five_one);
//个税计算:起征点5000元,应纳税所得额=工资-五险一金-5000
double tax_income = salary - five_one - 5000;
double tax = 0;
//月度个税税率表:≤3000 3%;3000-12000 10% 速算扣除数210
if (tax_income <= 3000)
{
tax = tax_income * 0.03;
}
else if (tax_income > 3000 && tax_income <= 12000)
{
tax = tax_income * 0.1 - 210;
}
Console.WriteLine("个人每月个税:" + tax);
//到手工资
double get_salary = salary - five_one - tax;
Console.WriteLine("税后到手工资:" + get_salary);
更多推荐



所有评论(0)