语句

语句:
三类:顺序、分支、循环


分支语句:
(一)
if(表达式) //表达式返回值是True或False
{
}
说明:
1.表达式返回的是bool值;
2.小括号和花括号后面不需要加分号。


(二)
if(表达式)
{
}
else
{
}
例:1.输入年龄,大于等于18显示成年,否则显示未成年。
2.你能跑过豹子么?接收能或者不能。

(三)
if(表达式)
{
}
else if
{
}
else if
{
}
...
else
{
}
各种情况只能走其中之一,若上面的都没走,将执行else里面的。

(四)
if(表达式)
{
if(){}
else{}
}
else
{
if(){}
}
if嵌套


////输出
//Console.Write("1234");
//Console.WriteLine("5678");

////输入
//string s= Console.ReadLine();
//int a = int.Parse(Console.ReadLine());


////数据类型:值类型,引用类型
////值类型:整数类型,浮点型,字符型,bool型
//int b = 2;
//double c = 3.14;
//decimal d = 3.14m;
//float e = 3.14f;
//char f = 'A';
//bool g = true;

////引用类型:字符串类型
//string h = "12345678";


////定义常量
//const int j = 5;
////j = 7;错误


////运算符
////算术运算符
////++ --
////int a = 8;
////int b = a++;
////int c = ++a;

////* / % 取余

////+ -

////关系运算符
////> < >= <=

////== !=

////逻辑运算符
////&& 与:两个都需要满足才是true
////|| 或:只要有一个满足就是true
////! 非:取相反的

////条件运算符
////? :
//int t = 7 > 9 ? 1 : 2;
//string ss = 8 < 10 ? "是" : "不是";

////赋值运算符
////= += -= *= /= %=
//int y = 5;
//y += 10;//y=y+10;


//老狼老狼几点了?
//输入小时数,判断是am还是pm
//若是pm,报时的时候减去12小时

Console.WriteLine("老狼老狼几点了");
int a = int.Parse(Console.ReadLine());
string b=a>12?(a-12)+"pm":a+"am";
Console.WriteLine(b);
Console.ReadLine();

//语句:顺序语句、分支语句(选择)、循环语句

//选择语句
//if(){}
//int a = 15;
//bool b = a < 10;
//if (b)//条件,返回true或false
//{
// Console.WriteLine("a是10以内的数!");
//}
//Console.ReadLine();

//if(){} else{} else表示跟if的条件完全相反
//二选一
//如果if没有执行,else就必须执行
//如果if执行了,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/zl1121102942/p/5598358.html