【C#——温习中体会你的三大结构】
来源:互联网 发布:美工笔记本配置2016 编辑:程序博客网 时间:2024/05/16 02:12
前言:
在学习C#的过程中,慢慢接触到了三大结构:顺序结构、选择结构、循环结构,在看视频的过程中,并自己动手去实现例子,总觉的似曾相识,好像已经学习过了,是的!知识的学习是个i+1的过程,以前的在学习VB 的时候,这三大结构给了我很深刻的印象,今天,我们来通过这篇文章来深刻的了解C#中的这三大循环结构。
主体:
回顾VB:
C#重温:
(一)顺序结构:
按照程序语句的编写顺序依次执行。
(二)选择结构
If:有可能一条语句都不执行
例子:
<strong><span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;">public static void CheckPos() { if (PlayerPos[0]>99) { PlayerPos[0] = 99; } if (PlayerPos[1]>99) { PlayerPos[1] = 99; } if (PlayerPos[0]<0) { PlayerPos[0] = 0; } if (PlayerPos[1]<0) { PlayerPos[1] = 0; } }</span></span></span></strong>
if-else:有条件的执行一条语句,至少执行一条语句
例子:
<strong><span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"> if (number == 1) { int temp = 0; temp = PlayerPos[playerPos]; PlayerPos[playerPos] = PlayerPos[1 - playerPos]; PlayerPos[1 - playerPos] = temp; msg=string.Format("玩家{0}选择了与玩家{1}交换位置", PlayerNames[playerPos], PlayerNames[1 - playerPos]); } else { PlayerPos[1 - playerPos] = 0; msg=string.Format("玩家{0}选择轰炸玩家{1}", PlayerNames[playerPos], PlayerNames[1 - playerPos]); }</span></span></span></strong>
if-else if:如图
例子:
<strong><span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"> if (PlayerPos[0] == PlayerPos[1] && PlayerPos[0] == pos) { Console.ForegroundColor = ConsoleColor.Yellow; temp = "<>"; } else if (PlayerPos[0] == pos)//如果玩家A在地图上就画A { Console.ForegroundColor = ConsoleColor.Yellow; temp = "A"; } else if (PlayerPos[1] == pos)//如果玩家B在地图上就画B { Console.ForegroundColor = ConsoleColor.Yellow; temp = "B"; }</span></span></span></strong>
switch-case:
<strong><span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"> switch (Map[pos])//如果玩家A和玩家B不在一起也不在这个坐标上就画该显示的地图图标 { case 0: Console.ForegroundColor = ConsoleColor.Gray; temp = "□"; break; case 1: Console.ForegroundColor = ConsoleColor.Red; temp = "◎"; break; case 2: Console.ForegroundColor = ConsoleColor.Blue; temp = "☆"; break; case 3: Console.ForegroundColor = ConsoleColor.Green; temp = "▲"; break; case 4: Console.ForegroundColor = ConsoleColor.Magenta; temp = "卐"; break; }//end switch</span></span></span></strong>
(三)循环结构
1.While:
例子:
<span style="font-size:18px;"><span style="font-size:18px;">while (PlayerNames[0]=="") { Console.WriteLine("玩家A的姓名不能为空,请重新输入"); PlayerNames[0] = Console.ReadLine(); } Console.WriteLine("请输入玩家B的姓名"); PlayerNames[1] = Console.ReadLine();</span></span>
2.do-while:
例子:
<strong><span style="font-size:18px;"><span style="font-size:18px;"> string name = ""; string pwd = ""; do { Console.WriteLine("请输入帐号"); name = Console.ReadLine(); Console.WriteLine("请输入密码"); pwd = Console.ReadLine(); } while (name != "admin" || pwd != "88888"); Console.WriteLine("程序结束"); Console.ReadKey();</span></span></strong>
3.for :知道循环次数,用for
例子:
<strong><span style="font-size:18px;"><span style="font-size:18px;"><span style="font-size:18px;"> for (int i = 65; i <= 69; i++) { Console.WriteLine(DrawStringMap(i)); }</span></span></span></strong>
循环中的跳转:
break
continue
总结:
学习是一个不断重复的过程,每学习一个新的知识,并不都是新的,我们尽可能用已有的知识学习它,在i+1的力量之上。
1 0
- 【C#——温习中体会你的三大结构】
- Java温习——三大注释
- C语言三大结构
- C语言三大结构
- 数据结构(1)—— 数据结构的三大结构
- C++ Primer温习体会
- 温习段三:C++中的C
- C#的三大结构
- C语言指针的温习
- 体会IBM的思维——参加2011年IBM大济南区高峰论坛体会摘要
- 温习C/C++笔记(2)——指针和引用的区别
- 温习C/C++笔记(3)——Vector和List的区别
- 深夜的你—开心编程之深夜体会
- 简单温习一下tomcat的目录结构
- 终于等到你!温习系统的重装!
- C语言中结构体的三种定义方式
- 结构之美——优先队列三大结构(三)——Pairing Heap
- 结构之美——优先队列三大结构(三)——Pairing Heap
- VMware虚拟机文件夹中各文件作用详解
- hdu 5610 Baby Ming and Weight lifting(思维,逻辑)
- 网络流学习笔记
- Android控制后台音乐播放器暂停,播放
- 编程基础——第一单元 变量、顺序和分支2
- 【C#——温习中体会你的三大结构】
- iOS中JavaScript和OC交互
- 天气预报应用
- 大话设计模式-组合模式
- java_69_annotation
- 编程基础——第二单元 循环
- 用Jquery的attr方法动态添加JSON对象参数
- SpringMVC接收json数据的demo
- this class is not key value coding-compliant for the key countryImageView-bug