语句
? 語(yǔ)句是指程序命令,都是按照順序執(zhí)行的。語(yǔ)句在程序中的執(zhí)行順序稱為“控制流”或者“執(zhí)行流”。根據(jù)程序?qū)\(yùn)行時(shí)所收到的輸入的響應(yīng),在程序每次運(yùn)行時(shí)控制流可能有所不同。
? 注意,語(yǔ)句間的標(biāo)點(diǎn)符號(hào)必須是英文標(biāo)點(diǎn),語(yǔ)句的結(jié)束標(biāo)點(diǎn)是分號(hào)“;”
? 語(yǔ)句可以嵌套,可以是以分號(hào)結(jié)尾的單行代碼,也可以是語(yǔ)句塊中的單行語(yǔ)句。語(yǔ)句塊括在括號(hào){}中,并且可以包含嵌套。
? ? 語(yǔ)句的類型包括聲明語(yǔ)句,表達(dá)式語(yǔ)句,選擇語(yǔ)句,循環(huán)語(yǔ)句,跳轉(zhuǎn)語(yǔ)句,異常語(yǔ)句。
一、選擇語(yǔ)句
? if,else
? if是如果的意思,else是另外的意思,if后面跟()括號(hào)內(nèi)為判斷條件,如果符合條件則進(jìn)入if語(yǔ)句執(zhí)行命令。如果不符合則不進(jìn)入if語(yǔ)句。else后不用加條件,但是必須與if配合使用,else后也可加if,但if后需要條件。If-else可以嵌套。
類似于條件運(yùn)算符,其格式有以下幾種:
格式1:if(...)//括號(hào)內(nèi)是判斷條件
? ? ? ? ? ? {
? ? ? ? ? ? ?//程序代碼,運(yùn)算等等
? ? ? ? ? ? ?}
?格式2:if()//括號(hào)內(nèi)為判斷條件
? ? ? ? ? ? {
? ? ? ? ? ? ?//程序代碼,運(yùn)算等等
? ? ? ? ? ? ?}
? ? ? ? ? ? ?else//如果不滿足條件則執(zhí)行這里的代碼
? ? ? ? ? ? ?{
? ? ? ? ? ? ? //程序代碼,運(yùn)算等等
? ? ? ? ? ? ? }
格式3:if(...)//括號(hào)內(nèi)為判斷條件
? ? ? ? ? {
? ? ? ? ? ?}
? ? ? ? ? ?else if(...)//另外如果滿足條件2則執(zhí)行以下的代碼
? ? ? ? ? ?{
? ? ? ? ? ??//程序代碼,運(yùn)算等等
? ? ? ? ? ? }
格式4:if(...)//如果滿足條件1則執(zhí)行這里的代碼
? ? ? ? ? ?{
? ? ? ? ? ??//程序代碼,運(yùn)算等等
? ? ? ? ? ? }
? ? ? ? ? ?if(...)//如果滿足條件2則執(zhí)行這里的代碼
? ? ? ? ? ? {
? ? ? ? ? ? ??//程序代碼,運(yùn)算等等
? ? ? ? ? ? ?}
? ? ? ? ? ? else//最后不滿足以上條件則執(zhí)行這里的代碼
? ? ? ? ? ? {
? ? ? ? ? ? ?//程序代碼,運(yùn)算等等
? ? ? ? ? ? ?}
?
//Console.Write("請(qǐng)輸入您的年齡:");
//int age = int.Parse(Console.ReadLine());
//if (age >= 18)
//{
// Console.WriteLine("成年");
//}
//else//另外的其他的所有條件 age<18
//{
// Console.WriteLine("未成年");
//}
//Console.ReadLine();
//if(){} else if(){}...else if(){} else{}
//多選一
//輸入性別
//Console.Write("請(qǐng)輸入您的性別:");
//string sex = Console.ReadLine();
//if (sex == "男")
//{
// Console.WriteLine("您是男性!");
//}
//else if (sex == "女")
//{
// Console.WriteLine("您是女性!");
//}
//else//sex!="男" sex!="女"
//{
// Console.WriteLine("輸入錯(cuò)誤!");
//}
//Console.ReadLine();
//if else的嵌套
//Console.Write("請(qǐng)輸入您的年齡:");
//int age = int.Parse(Console.ReadLine());
//if (age >= 0 && age <= 135)//人的正常年齡范圍
//{
// if (age <= 12)
// {
// Console.WriteLine("你是兒童!");
// }
// else if (age <= 18)
// {
// Console.WriteLine("你是青少年!");
// }
// else if (age <= 35)
// {
// Console.WriteLine("你是青年!");
// }
// else if (age <= 60)
// {
// Console.WriteLine("你是中年!");
// }
// else
// {
// Console.WriteLine("你是老年!");
// }
//}
//else//不屬于正常人的年齡范圍
//{
// Console.WriteLine("你是屬王八的么?");
//}
//Console.ReadLine();
?
//錯(cuò)誤!!!!!!!!!
//if()
//{}
//if(){}
//else{}
//你能跑過(guò)豹子么?接收能或者不能。
//Console.Write("你能跑過(guò)豹子么?");
//string ss = Console.ReadLine();
//if (ss == "能")
//{
// Console.WriteLine("你比禽獸還禽獸!");
//}
//else if (ss == "不能")
//{
// Console.WriteLine("你連禽獸都不如!");
//}
//else
//{
// Console.WriteLine("輸入有誤!");
//}
?
//第二種
//if (ss == "能" || ss == "不能")
//{
// if (ss == "能")
// {
// Console.WriteLine("你比禽獸還禽獸!");
// }
// else
// {
// Console.WriteLine("你連禽獸都不如!");
// }
//}
//else
//{
// Console.WriteLine("輸入有誤!");
//}
?
//輸入三個(gè)整數(shù),xyz,最終以從小到大的方式輸出。利用嵌套。
//Console.Write("請(qǐng)輸入x:");
//int a = int.Parse(Console.ReadLine());
//Console.Write("請(qǐng)輸入y:");
//int b = int.Parse(Console.ReadLine());
//Console.Write("請(qǐng)輸入z:");
//int c = int.Parse(Console.ReadLine());
//if (a < b && a < c )
//{
// if(b<c)
// {
// Console.WriteLine("三個(gè)數(shù)由小到大為" + a + b + c);
// }
// else
// {
// Console.WriteLine("三個(gè)數(shù)由小到大為" +a+c+b);
// }
//}
//else if(b<a&&b<c)
// {
// if(a<c)
// {
// Console.WriteLine("三個(gè)數(shù)由小到大為" + b + a + c);
// }
// else
// {
// Console.WriteLine("三個(gè)數(shù)由小到大為" + b + c +a);
// }
// }
// else//c是最小的
// {
// if (a < b)
// {
// Console.WriteLine("三個(gè)數(shù)由小到大為" + c + a + b);
// }
// else
// {
// Console.WriteLine("三個(gè)數(shù)由小到大為" + c + b + a);
// }
// }
?
?
//1.輸入學(xué)生姓名,輸入考試成績(jī) double
//若是100,【恭喜你***,滿分通過(guò)!】
//若是大于等于80小于100,【**,你很優(yōu)秀,繼續(xù)保持!】
//若是大于等于60小于80,【**成績(jī)良好】
//大于等于50小于60,【**就差一點(diǎn)點(diǎn),下次一定要至少及格!】
//小于50,【**你是笨蛋么?】
//Console.WriteLine("請(qǐng)輸入您的姓名");
//string a = Console.ReadLine();
//Console.WriteLine("請(qǐng)輸入你的考試成績(jī)");
//double b = double.Parse(Console.ReadLine());
//if (b >= 0 && b <= 100)
//{
// if (b == 100)
// {
// Console.WriteLine("恭喜你" + a + ",滿分通過(guò)");
// Console.ReadLine();
// }
// else if (b < 100 && b >= 80)
// {
// Console.WriteLine(a + ",你很優(yōu)秀,繼續(xù)保持");
// Console.ReadLine();
// }
// else if (b < 80 && b >= 60)
// {
// Console.WriteLine(a + "成績(jī)良好");
// Console.ReadLine();
// }
// else if (b < 60 && b >= 50)
// {
// Console.WriteLine(a + "就差那么一點(diǎn)點(diǎn),下次一定要及格");
// Console.ReadLine();
// }
// else//b<50
// {
// Console.WriteLine(a + "你是笨蛋嗎?");
// Console.ReadLine();
// }
//}
//else
//{
// Console.WriteLine("輸入錯(cuò)誤");
//}
?
?
// 有一組函數(shù):y = x (x<1);
//y = 2x -1 (1<=x<10);
//y = 3x-11 (x>=10)。
//括號(hào)內(nèi)是x的滿足條件。
//實(shí)現(xiàn)功能,隨意輸入一個(gè)x值,輸出y的值。
//Console.WriteLine("請(qǐng)輸入一個(gè)x的值:");
//double x = double.Parse(Console.ReadLine());
//if (x < 1)
//{
// Console.WriteLine(x);
//}
//else
//{
// if (x>=1&&x<10)
// {
// Console.WriteLine(2*x-1);
// }
// else
// {
// if (x>=10)
// {
// Console.WriteLine(3*x-11);
// }
// }
// Console.ReadLine ();
?
?
//3.輸入整數(shù)a和b,若a2+b2大于100,則輸出a2+b2百位以上數(shù)字,
//否則輸出兩數(shù)之和
//Console.Write("請(qǐng)輸入整數(shù)a:");
//int a = int.Parse(Console.ReadLine());
//Console.Write("請(qǐng)輸入整數(shù)b:");
//int b = int.Parse(Console.ReadLine());
//if ((a * a + b * b) > 100)
//{
// Console.WriteLine(a * a + b * b);
//}
//else//a*a+b*b<=100
//{
// Console.WriteLine(a + b);
//}
//Console.ReadLine();
?
//4.相親過(guò)程:你有房子么?你有錢么?你有能力么?
//【結(jié)婚吧】【先買房子在結(jié)婚】
//【先賺錢再買房子再結(jié)婚】都沒有【拜拜~~】
//利用if嵌套做相親過(guò)程
//Console.Write("你有房子么?");
//string h = Console.ReadLine();
//if (h == "有")
//{
// Console.WriteLine("結(jié)婚吧");
//}
//else
//{
// Console.Write("你有錢么");
// string j = Console.ReadLine();
// if (j == "有")
// {
// Console.WriteLine("先買房子再結(jié)婚");
// }
// else
// {
// Console.Write("你有能力么");
// string k = Console.ReadLine();
// if (k == "有")
// {
// Console.WriteLine("先賺錢再買房子再結(jié)婚");
// }
// else
// {
// Console.WriteLine("拜拜~~");
// }
// }
//}
//Console.ReadLine();
?
//輸入一個(gè)年份,判斷是否是閏年
//(能被4整除卻不能被100整除的年份。a%4==0&&a%100!=0
//世紀(jì)年份能被400整除的是閏年)a%400==0
//Console.Write("請(qǐng)輸入一個(gè)年份:");
//int year = int.Parse(Console.ReadLine());
//if (year >= 0 && year <= 9999)
//{
// if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
// {
// Console.WriteLine("您輸入的年份是閏年!您輸入的年份是:{0}", year);
// }
// else
// {
// Console.WriteLine("您輸入的年份是平年!您輸入的年份是:"+year);
// }
//}
//else
//{
// Console.WriteLine("輸入有誤!");
//}
//Console.ReadLine();
?
//輸入年、月、日,判斷時(shí)間日期格式是否正確
//年:0~9999
//月:1~12
//日:1. 1 3 5 7 8 10 12 31天
// 2. 4 6 9 11 30天
// 3. 2 (1)閏年:29天 (2)平年:28天
//(能被4整除卻不能被100整除的年份。a%4==0&&a%100!=0
//世紀(jì)年份能被400整除的是閏年)a%400==0
Console.Write("請(qǐng)輸入年份:");
int year = int.Parse(Console.ReadLine());
if (year >= 0 && year <= 9999)
{
Console.Write("請(qǐng)輸入月份:");
int month = int.Parse(Console.ReadLine());
if (month >= 1 && month <= 12)
{
Console.Write("請(qǐng)輸入日:");
int day = int.Parse(Console.ReadLine());
if (day >= 1 && day <= 31)
{
//31天的月份
if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
{
Console.WriteLine("輸入的日期格式正確!您輸入的日期為:{0}-{1}-{2}", year, month, day);
}
else //4,6,9,11,2
{
if (month == 4 || month == 6 || month == 9 || month == 11)
{
if (day <= 30)
{
Console.WriteLine("輸入的日期格式正確!您輸入的日期為:{0}-{1}-{2}", year, month, day);
}
else
{
Console.WriteLine("輸入有誤!");
}
}
else//2
{
if (day <= 28)
{
Console.WriteLine("輸入的日期格式正確!您輸入的日期為:{0}-{1}-{2}", year, month, day);
}
else//29,30,31
{
if (day == 29)
{
if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
{
Console.WriteLine("輸入的日期格式正確!您輸入的日期為:{0}-{1}-{2}", year, month, day);
}
else
{
Console.WriteLine("輸入有誤!");
}
}
else//30,31
{
Console.WriteLine("輸入有誤!");
}
}
}
}
}
else
{
Console.WriteLine("輸入的日期有誤!");
}
}
else
{
Console.WriteLine("輸入的月份有誤!");
}
}
else
{
Console.WriteLine("輸入的年份有誤!");
}
Console.ReadLine();
轉(zhuǎn)載于:https://www.cnblogs.com/dreamer666/p/5600779.html
創(chuàng)作挑戰(zhàn)賽新人創(chuàng)作獎(jiǎng)勵(lì)來(lái)咯,堅(jiān)持創(chuàng)作打卡瓜分現(xiàn)金大獎(jiǎng)總結(jié)
- 上一篇: awk用法:取列表最后一列
- 下一篇: 毕业后才认清的15个道理