一、顺序语句

二、条件,分支语句

1、if语句

关键是能够熟练运用 if的嵌套。要考虑好所有的情况。

如果说 条件是两种+情况相互对应的,那么就可以只用 if 与else 。但必须要想好 每个else 跟哪个if是一对。

如果情况是相互独立的三种情况以上,那么可以选择运用if ... else if ...else。

1.if语句

if(条件)
{
满足条件的时候执行;
}

2. if(条件)

{
满足条件执行;
}
else
{
不满足条件时执行;
}

3 if(条件1)
{
满足条件1的时候执行;
}
else if(条件2)
{
不满足条件1的情况下满足条件2;
}

4.

if(条件1)
{
if(条件2)
{
既满足条件1又满足条件2的时候执行;
}
}

eg 1.判断年份是否为闰年?格式是否正确?

    static void Main5(string[] args)
{
Console.WriteLine("请输入年份:");
int nian = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入月份:");
int yue = Convert.ToInt32(Console.ReadLine()); Console.WriteLine("请输入日期:");
int ri = Convert.ToInt32(Console.ReadLine()); int r = ; //r=0代表平年 r=1代表闰年 if (nian > && nian < )
{
//判断闰年还是平年
if (nian % == )
{
if (nian % == )
{
r = ;
Console.WriteLine("该年是闰年");
}
else
{
Console.WriteLine("该年是平年");
}
}
else
{
if (nian % == )
{
r = ;
Console.WriteLine("该年是闰年");
}
else
{
Console.WriteLine("该年是平年");
}
} //判断月份
if (yue >= && yue <= )
{
//判断日期是否合法
if(yue== || yue== || yue == || yue== || yue== || yue== ||yue == )
{
if(ri<= && ri>)
{
Console.WriteLine("输入的日期正确!");
}
else
{
Console.WriteLine("输入的日期不正确!");
}
}
else if(yue == || yue == || yue== || yue==)
{
if(ri<= && ri>)
{
Console.WriteLine("输入的日期正确!");
}
else
{
Console.WriteLine("输入的日期不正确!");
}
}
else
{
if(r == )
{
if(ri> && ri<=)
{
Console.WriteLine("输入的日期正确!");
}
else
{
Console.WriteLine("输入的日期不正确!");
}
}
else
{
if(ri> && ri<=)
{
Console.WriteLine("输入的日期正确!");
}
else
{
Console.WriteLine("输入的日期不正确!");
}
}
} }
else
{
Console.WriteLine("输入的月份不正确,日期有假!");
} }
else
{
Console.WriteLine("输入的年份不正确!日期有假");
}
}

2、switch 语句

如果说可选的条件比较多时,选择switch语句,要比if语句效率要高。特别注意的是 case 后跟的break。

eg:

  //eg.6 swtich语句   作用域
static void Maine(string[] args)
{
//Console.WriteLine("你本次选择出场的英雄是:");
Random r = new Random();
int n = r.Next(); string a; switch (n)
{
case :
a = "赵信"; break;
case :
a = "寒冰射手";break;
case :
a = "无极剑圣";break;
case :
a = "机器人"; break;
default:
a = "齐天大圣";break;
}
Console.WriteLine("本次选择的英雄是:"+a);
}

三、循环语句

for循环

四要素:

初始条件,循环条件,状态改变,循环体。 执行过程:

初始条件--循环条件--循环体--状态改变--循环条件....

注意:for的小括号里面分号隔开,for的小括号后不要加分号。

利用 加断点的方式,可以更好的明白for的工作原理。

1.for循环空操作完成的实例, 输出100以内的数字

 static void Main(string[] args)
{
int i = ;
for (; ; )
{
if (i > )
{
break;
}
Console.Write(i + "\t");
i++;
}
Console.ReadKey();
}

当然你也可以用 while,if() break;的嵌套完成上述操作。

2.正序和逆序的推断问题。 (折纸问题)

  //eg.5 折纸问题
static void Maine(string[] args)
{
//Console.WriteLine("请输入次数");
//int n = Convert.ToInt32(Console.ReadLine()); //int i = 0;
//for (double sum = 0.0001; sum <= 8848.0; sum = sum * 2)
//{
// i++; //}
//Console.WriteLine(i); double sum = 0.0001;
int z = ; for (int i = ; ; i++)
{
z++;
sum = sum * ; if (sum >= 8848.0)
{
Console.WriteLine(z);
break;
}
}
}

3.应用:a.穷举法: 用循环把各种可能的情况都给走一遍,然后用if条件把满足要求的结果给筛选出来。

 //eg.6 百马百石 大马驮2石,中马驮1石 小马驮0.5石
static void Main6a(string[] args)
{
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
for (int k = ; k <= ; k++)
{
if ( (i * + j * + k * 0.5 == ) && (i + j + k == ) )
{
Thread.Sleep();
Console.WriteLine("大马需要" + i + "头,中马需要" + j + "头,小马需要" + k + "头。");
}
}
}
}
}
//eg.7
static void Maing(string[] args)
{
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
for (int k = ; k < ; k++)
{
if (i * + j * + k * == )
{
Console.WriteLine("50元用来买" + i.ToString() + "个牙刷," + j.ToString() + "个牙膏," + k.ToString() + "块肥皂,正好能用完。");
}
}
}
} }
//eg.8 有1块,2块,5块的钱若干,凑出20块钱,有几种凑法
static void Mainh(string[] args)
{
int m = ;
for (int i = ; i <= ; i++)
{
for (int j = ; j <= ; j++)
{
for (int k = ; k < ; k++)
{
if (i * + * j + * k == )
{
m++;
Console.WriteLine("一共有" + m + "中方法。");
Console.WriteLine("需要1元的" + i + "张,2元的" + j + "张,5元的" + k + "张。");
}
}
}
}
}
//eg.9 1 () 2 () 3 ()4 = 4;问括号里我要填 (- 或 +)
static void Maini(string[] args)
{
for (int i = ; i <= ; i += )
{
for (int j = -; j <= ; j += )
{
for (int k = -; k <= ; k += )
{
for (int l = -; l <= ; l += )
{
if ( * i + * j + * k + l * == )
{
Console.WriteLine("i=" + i + ",j=" + j + ",k=" + k + ",l=" + l + "。");
}
} }
}
}
}
//eg.10 123()45()67()8()9=100;要求在()里面填写+或-使等式成立。
static void Maini2(string[] args)
{
for (int a = -; a <= ; a += )
{
for (int b = -; b <= ; b += )
{
for (int c = -; c <= ; c += )
{
for (int d = -; d <= ; d += )
{
if ( + a * + b * + c * + d * == )
Console.WriteLine("a=" + a + ",b=" + b + ",c=" + c + ",d=" + d);
}
}
}
}
Console.ReadKey();
}
//eg.11 某侦查队接到一项紧急任务,要求在A.B.C,D,E,F六名队员中尽可能多的挑选若干人。A和B两人必须去一人。A和D不能同时去。A,E,F三人必须两人去。B和C都
//去或都不去。C和D两人中去一人。若D不去,E也不去。问应叫哪几个人去?(灵活运用1与0)
         static void Mainj(string[] args)
{
for (int a = ; a <= ; a++)
{
for (int b = ; b <= ; b++)
{
for (int c = ; c <= ; c++)
{
for (int d = ; d <= ; d++)
{
for (int e = ; e <= ; e++)
{
for (int f = ; f <= ; f++)
{
if ((a + b >= ) && (a + d <= ) && (a + e + f == ) && (b + c != ) && (c + d == ) && (d - e >= ))
{
Console.WriteLine("A=" + a + "B=" + b + "C=" + c + "D=" + d + "E=" + e + "F=" + f);
} }
}
}
}
}
}
}
//老师版
static void Mainj1(string[] args)
{
int a, b, c, d, e, f;
for (a = ; a < ; a++)
{
for (b = ; b < ; b++)
{
for (c = ; c < ; c++)
{
for (d = ; d < ; d++)
{
for (e = ; e < ; e++)
{
for (f = ; f < ; f++)
{
if ((a + b >= ) && (a + d <= ) && (a + e + f == ) && (b + c != ) && (c + d == ) && ((d + e == ) || d == ))
{
Console.WriteLine("A=" + a + "B=" + b + "C=" + c + "D=" + d + "E=" + e + "F=" + f);
}
}
}
}
}
}
}
Console.ReadKey();
}

b.迭代法:有一定规律。 每次循环都是从上次运算结果中获得数据,本次运算的结果都是要为下次运算做准备。

eg1 兔生兔问题

有一对幼兔,幼兔一个月后成长为小兔,小兔一个月后成长为成兔并生下一对幼兔,问几年后有多少对兔子,其中幼兔,小兔,成兔分别是多少?

 //eg.2 兔生兔问题

         //方法一
static void Maink3(string[] args)
{
int syt = , byt = ;
int sxt = , bxt = ;
int sct = , bct = ; Console.WriteLine("请输入月数:");
int month = Convert.ToInt32(Console.ReadLine());
int sum; for (int i = ; i <= month; i++)
{
//赋值顺序不能变,必须按照兔子生长规律来,先有的bct才会有byt
bct = sxt + sct;
bxt = syt;
byt = sxt + sct; //bct = sxt + sct; 这样写,必须注意他的顺序
//bxt = syt;
//byt = bct; //byt = bct;//错误的
//bxt = syt;
//bct = sxt + sct; syt = byt;
sxt = bxt;
sct = bct; //sum = byt + bxt + bct;
} sum = byt + bxt + bct; Console.WriteLine("过了{0}个月后,幼兔个数为{1}对,小兔个数为{2}对,成兔个数为{3}对,总共有{4}对。", month.ToString(), byt, bxt, bct,sum); }
//方法二
static void Maink4(string[] args)
{
int n = Convert.ToInt32(Console.ReadLine());
int tu = ;//要求那个月的总数
int tu1=, tu2=;//倒数第一个为 tu1,倒数第二个为 tu2 for (int i = ; i < n;i++ )
{
tu = tu1 + tu2;
tu2 = tu1;
tu1 = tu;
}
Console.WriteLine(tu); }
//方法三
static void Maink5(string[] args)
{
Console.Write("请输入月数:");
int m = int.Parse(Console.ReadLine());
int ct = ;//成兔的对数
int xt = ;//小兔的对数
int yt = ;//
int zt = ;// for (int i = ; i <= m; i++)
{
if (i == )
{
ct = ;
xt = ;
yt = ;
}
else
{
ct = xt + ct;
xt = yt;
yt = ct;
}
zt = yt + xt + ct; Console.WriteLine(i.ToString() + "个月后成兔的对数是:" + ct.ToString());
Console.WriteLine(i.ToString() + "个月后小兔的对数是:" + xt.ToString());
Console.WriteLine(i.ToString() + "个月后幼兔的对数是:" + yt.ToString());
Console.WriteLine(i.ToString() + "个月后兔子的总对数是:" + zt.ToString());
}
Console.ReadLine();
}

eg 2  100以内的所有数的和。

eg3. 求阶乘。
eg4.求年龄。
eg5.折纸。
eg6.棋盘放粮食。
eg7.猴子吃桃子。

   static void Maink(string[] args)
{
int sum = ;
for (int i = ; i < ; i++)
{
int t = (sum + ) * ;
sum = t;
}
Console.WriteLine("桃子一共有:" + sum + "个。");
}


eg8.落球问题。一个球从10米高度落下,每次弹起2/3的高度,问第五次弹起后的高度?

eg.9

打印菱形

 //eg.2 打印菱形(教材版)
static void Mainb(string[] args)
{
//菱形上半部
Console.WriteLine("请输入一个数:");
int n = int.Parse(Console.ReadLine());
for (int i = ; i <= n; i++)
{
for (int b = ; b <= n - i; b++)
{
Console.Write(" ");
}
for (int j = ; j <= * i - ; j++)
{
Console.Write("☆");
}
Console.Write("\n");
} //菱形下半部
for (int p = ; p < n; p++)
{
for (int o = ; o <= p; o++)
{
Console.Write(" ");
}
for (int k = ; k <= * (n - p) - ; k++)
{
Console.Write("★");
}
Console.Write("\n");
}
Console.ReadKey();
} //eg.4 打印菱形(自己做)
static void Maind(string[] args)
{
Console.WriteLine("请输入要打印菱形的中间层个数:");
int a = Convert.ToInt32(Console.ReadLine()); //组成菱形必须是奇数行
if (a % != )
{
int c = (a + ) / ;
//上半部
for (int i = ; i <= c; i++)
{
for (int j = i; j <= c - ; j++)
{
Console.Write(" ");
}
for (int k = ; k <= * i - ; k++)
{
Console.Write("★");
}
Console.WriteLine();
} //下半部
for (int m = c + ; m <= a; m++)
{
for (int n = ; n <= m - c; n++)
{
Console.Write(" ");
}
for (int o = * (a - m) + ; o >= ; o--)
{
Console.Write("☆");
}
Console.WriteLine();
}
}
else
{
Console.WriteLine("你所输入的层数不可能组成菱形。");
} }

四、while 循环。一般用在一些死循环当中。

五、try catch。保护程序,避免程序出错时无法运行。

格式:

 try//快捷方式:双击 tab键
{ }
catch (Exception)
{ throw;
}
finally
{ }

六、跳转语句

1.break。跳出循环体。

eg

如果循环有多层嵌套,停止的是最贴近break的那个循环,即循环2
for()循环1
{
for()循环2
{
if()
{
break
}
}
}

2.continue。跳出本次循环,进行下一次循环。

C# 课堂总结3-语句的更多相关文章

  1. 课堂笔记——循环语句-for

    一.循环:多次执行某段代码. 二.循环四要素: 1.初始条件 2.循环条件 3.状态改变 4.循环体 三.for循环 1.语法: for(初始条件;循环条件;状态改变)       { 循环体 } 2 ...

  2. python 课堂笔记-for语句

    for i in range(10): print("----------",i) for j in range(10): print("world",j) i ...

  3. python 课堂笔记-if语句

    # Author:zyl _username = 'zyl' _password = 'zyl123' username = input("username:") password ...

  4. python第六天 函数 python标准库实例大全

    今天学习第一模块的最后一课课程--函数: python的第一个函数: 1 def func1(): 2 print('第一个函数') 3 return 0 4 func1() 1 同时返回多种类型时, ...

  5. whdxlib

    1 数据库系统实现 实 验 指 导 书 齐心 彭彬 计算机工程与软件实验中心 2016 年 3 月2目 录实验一.JDBC 应用程序设计(2 学时) ......................... ...

  6. #9.5课堂JS总结#循环语句、函数

    一.循环语句 1.for循环 下面是 for 循环的语法: for (语句 1; 语句 2; 语句 3) { 被执行的代码块 } 语句 1 在循环(代码块)开始前执行 语句 2 定义运行循环(代码块) ...

  7. 2017-2-22 if语句 if语句的嵌套 以及课堂练习 经典猜拳游戏,闰年判断,输出当前时间的天数等

    (一)if语句 1.格式   if(){ }else if() { } 注意:如果if后面不写花括号,只执行下面第一句 (二)语句1:顺序语句 2:循环语句 3:分支语句 课后练习: 1.猜拳游戏(用 ...

  8. 6.1课堂笔记—DML(数据操作语言),DQL查询语句

    一.DML(数据操作语言) InnoDB MyISAM 支持事务 不支持事务 不支持全文索引 支持全文索引 支持外键约束 不支持 命令查看默认存储引擎 show variables like '%st ...

  9. 松软科技课堂:SQL-SELECT-INTO语句

    SQL SELECT INTO 语句可用于创建表的备份复件. SELECT INTO 语句 SELECT INTO 语句从一个表中选取数据,然后把数据插入另一个表中. SELECT INTO 语句常用 ...

  10. 松软科技web课堂:JavaScript If...Else 语句

    条件语句用于基于不同条件执行不同的动作. 条件语句 在您写代码时,经常会需要基于不同判断执行不同的动作. 您可以在代码中使用条件语句来实现这一点. 在 JavaScript 中,我们可使用如下条件语句 ...

随机推荐

  1. urllib2.URLError: <urlopen error [Errno 104] Connection reset by peer>

    http://www.dianping.com/shop/8010173 File "综合商场1.py", line 152, in <module>    httpC ...

  2. BZOJ 1143 [CTSC2008]祭祀river(二分图匹配)

    [题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1143 [题目大意] 给出一张有向图,问最大不连通点集,连通具有传递性 [题解] 我们将 ...

  3. 清风注解-Swift程序设计语言:Point1~5

    目录索引 清风注解-Swift程序设计语言 Point 1. Swift 风格的"Hello, world" 代码事例: println("Hello, world&qu ...

  4. HDU 4661 Message Passing 【Tree】

    题意: 给一棵树,每一个结点都有一个信息,每一个时刻,某一对相邻的结点之间可以传递信息,那么存在一个最少的时间,使得所有的节点都可以拥有所有的信息.但是,题目不是求最短时间,而是求最短时间的情况下,有 ...

  5. new Date().getTime()

    new Date().getTime()获取当前时间戳; 不知道是毫秒级还是千分之一毫秒级别

  6. 【和我一起学习Unity3D】Unity3D的坐标控制

    坐标这个东西,在Unity3D里面是分为几个类的,各自是Vector2,Vector3.Vector4:含义各自是:二维坐标系,三维坐标系,四维坐标系.一般做游戏呢,用到的最多的就是Vector3了. ...

  7. winfrom播放动态图片

    winfrom是不能直接加载的动态图片的.只能够自己写方法实现. 具体代码如下: using System; using System.Collections.Generic; using Syste ...

  8. SQL Server索引进阶第十一篇:索引碎片分析与解决

    相关有关索引碎片的问题,大家应该是听过不少,也许也很多的朋友已经做了与之相关的工作.那我们今天就来看看这个问题. 为了更好的说明这个问题,我们首先来普及一些背景知识. 知识普及 我们都知道,数据库中的 ...

  9. 软件介绍:搜索工具 Listary

    如今的互联网时代,搜索的重要性我想大家都是认可的.网上的知识浩如烟海,而搜索引擎是通向这些知识的入口.谷歌.百度等搜索引擎给我们带来了极大的便利,也无怪他们成长为如今的互联网巨头. 然而储存在个人硬件 ...

  10. JavaScript中的构造函数

    目录: constructor & prototype 为构造函数添加属性和方法的多种不同方法 组合使用this和prototype关键字创建构造函数(常用方法) 用对象直接量作为构造函数的参 ...