分支嵌套
練習(xí)1
男士體重 = 身高 - 100 (+-3)
女士體重 = 身高 - 110 (+-3)
請(qǐng)輸入性別:
請(qǐng)輸入體重(kg):
請(qǐng)輸入身高(cm):
您的體重非常標(biāo)準(zhǔn)/您需要減肥了/您需要補(bǔ)充營(yíng)養(yǎng)
您距離標(biāo)準(zhǔn)體重還差xxx公斤
//輸入性別,體重和身高,判斷是否是標(biāo)準(zhǔn)體重,不是那相差多少公斤,你的體重很標(biāo)準(zhǔn)/你需要減肥啦/你需要補(bǔ)充營(yíng)養(yǎng)//用戶(hù)輸入性別,體重升高Console.Write("請(qǐng)輸入性別(男/女):");string a = Console.ReadLine();Console.Write("請(qǐng)輸入體重(kg):");decimal b = Convert.ToDecimal(Console.ReadLine());Console.Write("請(qǐng)輸入身高(cm):");decimal c = Convert.ToDecimal(Console.ReadLine());//判斷是否是標(biāo)準(zhǔn)體重并打印//男:體重=身高-100(+-3)// b =c-100(+-3) //女:體重=身高-110(+-3)//b=c-100(+-3)if (a == "男")//判斷是男是女 {decimal d = b - (c - (100));//與標(biāo)準(zhǔn)體重相差的if (d>3){Console.WriteLine("你需要減肥啦!");Console.WriteLine("與標(biāo)準(zhǔn)體重相差:"+(d-3)+"kg");}else if (d<-3){Console.WriteLine("你需要補(bǔ)充營(yíng)養(yǎng)!");Console.WriteLine("與標(biāo)準(zhǔn)體重相差:" +( d+3) + "kg");}else if(d>=-3&&d<=3){Console.WriteLine("你的體重非常標(biāo)準(zhǔn)!");}}else{decimal d = b - (c - 110);if (d>3){Console.WriteLine("你需要減肥啦!");Console.WriteLine("與標(biāo)準(zhǔn)體重相差:" + (d-3) + "kg");}else if (d<-3){Console.WriteLine("你需要補(bǔ)充營(yíng)養(yǎng)!");Console.WriteLine("與標(biāo)準(zhǔn)體重相差:" + (d+3) + "kg");}else if (d>=-3&&d<=3){Console.WriteLine("你的體重非常標(biāo)準(zhǔn)!");}}Console.Read();運(yùn)算結(jié)果
練習(xí)2
讓用戶(hù)輸入年齡,如果小于18歲,那么提示“少兒不宜,你不能查看。”
如果大于18歲,那么提示“你可以查看,是否要看?(yes/no):”
當(dāng)用戶(hù)輸入yes時(shí),輸出“查看成功”,否則輸出“用戶(hù)取消查看”。
運(yùn)算結(jié)果
練習(xí)3
24時(shí)轉(zhuǎn)為12時(shí)
讓用戶(hù)輸入一個(gè) 24時(shí)制的時(shí)間,比如:
請(qǐng)輸入小時(shí): 14
請(qǐng)輸入分鐘: 33
打印的結(jié)果為: 下午2點(diǎn)33分
//24時(shí)轉(zhuǎn)為12時(shí),小時(shí)不能為負(fù)數(shù),不能超過(guò)24.分鐘不能為負(fù)數(shù),不能超過(guò)60//用戶(hù)輸入一個(gè)24時(shí)制得時(shí)間 Console.Write("請(qǐng)輸入小時(shí):");int a = Convert.ToInt32(Console.ReadLine());Console.Write("請(qǐng)輸入分鐘:");int b = Convert.ToInt32(Console.ReadLine());//判斷是否輸入錯(cuò)誤,并打印if (a >= 0 && a < 24 )//小時(shí)輸入正確 {if (b > 0 && b < 60)//分鐘輸入正確 {if (a <= 12){Console.WriteLine("上午"+a + "點(diǎn):" + b + "分。");}else{int c = a - 12;Console.WriteLine("下午"+c + "點(diǎn):" + b + "分。");}}else{Console.WriteLine("分鐘輸入錯(cuò)誤!");}}else{Console.WriteLine("小時(shí)輸入錯(cuò)誤!");}Console.ReadLine();運(yùn)算結(jié)果
練習(xí)4
讓用戶(hù)輸入兩個(gè)數(shù),再讓用戶(hù)輸入一個(gè)運(yùn)算符,然后輸出運(yùn)算的結(jié)果
//輸入兩個(gè)數(shù),再輸入一個(gè)運(yùn)算符,然后輸出結(jié)果//用戶(hù)輸出兩個(gè)數(shù)Console.Write("請(qǐng)輸入第一個(gè)數(shù):");decimal a = Convert.ToDecimal(Console.ReadLine());Console.Write("請(qǐng)輸入第一個(gè)數(shù):");decimal b = Convert.ToDecimal(Console.ReadLine());Console.Write("請(qǐng)輸入一個(gè)運(yùn)算符:");string c = Console.ReadLine();//輸入一個(gè)運(yùn)算符if (c == "+" || c == "-" || c == "*" || c == "/" || c == "%"){if (c == "+"){Console.WriteLine(a+b);}else if (c == "-"){Console.WriteLine(a-b);}else if (c == "*"){Console.WriteLine(a*b);}else if (c == "/"){Console.WriteLine(a / b);}else if (c == "%"){Console.WriteLine(a % b);}}else{Console.WriteLine("輸入錯(cuò)誤");}Console.ReadLine();?
運(yùn)算結(jié)果
練習(xí)5
你有房子嗎?有,那結(jié)婚吧,沒(méi)有,你有錢(qián)嗎?有,先買(mǎi)房再結(jié)婚吧,沒(méi)有,你又能力嗎?有,先賺錢(qián)再買(mǎi)房再結(jié)婚吧,沒(méi)有拜拜
//輸入你有房嗎?有,那結(jié)婚吧。沒(méi)有,那你有錢(qián)嗎?有,那你先買(mǎi)房。沒(méi)有,那你有能力嗎?有,先賺錢(qián)再買(mǎi)房再結(jié)婚,沒(méi)有,拜拜//用戶(hù)輸入Console.Write("你有房嗎?(有/沒(méi)有)");string a = Console.ReadLine();//判斷,輸出if (a == "有")//有房 {Console.WriteLine("那結(jié)婚吧。");}else{Console.Write("你有錢(qián)嗎?");string b = Console.ReadLine();if (b == "有")//有錢(qián) {Console.WriteLine("先買(mǎi)房再結(jié)婚吧。");}else{Console.Write("那你有能力嗎?");string c = Console.ReadLine();if (c == "有")//有能力 {Console.WriteLine("那先賺錢(qián)再買(mǎi)房再結(jié)婚吧。");}else{Console.WriteLine("拜拜");}}}Console.ReadLine();運(yùn)算結(jié)果
練習(xí)6
?讓用戶(hù)輸入日期,判斷輸入的日期是否正確
0-9999,月1-12,日按照月份來(lái),需要判斷閏年 //輸入日期,判斷日期是否正確//用戶(hù)輸入Console.Write("請(qǐng)輸入年:");int a =Convert.ToInt32(Console.ReadLine());Console.Write("請(qǐng)輸入月份:");int b = Convert.ToInt32(Console.ReadLine());Console.Write("請(qǐng)輸入日期:");int c = Convert.ToInt32(Console.ReadLine());//判斷是否正確//1-9999//能被4整除的大部分是閏年,能被100整除而不能被400整除的不是閏年,能被3200整除的不是閏年int d = a % 4, e=a%100, f =a%400, s=a%3200;if (a > 0 && a <= 9999)//年份輸入正確 {if (d != 0&& e != 0 && f != 0 || s == 0)//不是閏年 {if (b > 0 && b < 12)//月份輸入正確 {if ((c < 0) || (b == 2 && c > 28)|| ((b == 1 || b == 3 || b == 5 || b == 7 || b == 8 || b == 10 || b == 12) && (c > 31))|| ((b == 4 || b == 6 || b == 9 || b == 11) && (c > 30)))//日期輸入不正確 {Console.WriteLine("日期輸入錯(cuò)誤");}else{Console.WriteLine("你輸?shù)娜掌谑?#xff1a;" + a + "年" + b + "月" + c + "日");}}else{Console.WriteLine("月份輸入錯(cuò)誤");}}else{if (b > 0 && b < 12)//月份輸入正確 {if ((c < 0) || (b == 2 && c > 29)|| ((b == 1 || b == 3 || b == 5 || b == 7 || b == 8 || b == 10 || b == 12) && (c > 31))|| ((b == 4 || b == 6 || b == 9 || b == 11) && (c > 30)))//日期輸入錯(cuò)誤 {Console.WriteLine("日期輸入錯(cuò)誤");}else{Console.WriteLine("你輸?shù)娜掌谑?#xff1a;" + a + "年" + b + "月" + c + "日");}}else{Console.WriteLine("月份輸入錯(cuò)誤");}}}else{Console.WriteLine("年份輸入錯(cuò)誤");}Console.ReadLine();運(yùn)算結(jié)果
?
轉(zhuǎn)載于:https://www.cnblogs.com/sunshuping/p/5515330.html
新人創(chuàng)作打卡挑戰(zhàn)賽發(fā)博客就能抽獎(jiǎng)!定制產(chǎn)品紅包拿不停!總結(jié)
- 上一篇: 我的NopCommerce之旅(4):
- 下一篇: jQuery API 3.1.0 速查表