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