C#之结构与枚举

来源:互联网 发布:退火算法用途 编辑:程序博客网 时间:2024/05/16 23:40

在学习VB时我们都知道VB有三大结构:顺序、选择、循环;也听说过枚举函数,那么在C#中我们又将接触到这类知识,其实在C#中是一样的,语言都是相通的,他们有共性,更有区别,不然他不会讲语言分这么清楚,这篇博客小编主要给大家提供一些C#中结构和枚举的知识,希望对大家有用。

一、C#中的结构:

1、顺序结构:即代码从上到下依次执行。

2、分支结构:If,if-else,if-else if,switch-case


下面通过一个小例子来让大家认识一下这几种用法:


If结构到if Else 结构再到if -else if结构的简化:
(1)If结构:

            Console.WriteLine("请输入成绩");            int score = Convert.ToInt32(Console.ReadLine());            if (score >= 90)            {                Console.WriteLine("A");            }            else            {                if (score >= 80)                {                    Console.WriteLine("B");                }                else                {                    if (score >= 70)                    {                        Console.WriteLine("C");                    }                    else                    {                        Console.WriteLine("D");                    }                }           }


(2)用If结构写比较繁琐,我们来用Else if简化一下:

            if (score >= 90)            {                Console.WriteLine("A");            }            else if(score >= 80)            {                    Console.WriteLine("B");            }               else if(score >= 70)            {                    Console.WriteLine("C");            }            else             {                    Console.WriteLine("D");            }

(3)还有一种方法,用后更简单,减少代码冗余:

            string str = "";            Console.WriteLine("请输入成绩");            int score = Convert.ToInt32(Console.ReadLine());                       if (score >= 90)            {                str="A";            }            else if (score >= 80)            {                str="B";            }            else if (score > 70)            {                str="C";            }            else            {                str="D";            }            Console.WriteLine(str);            Console.ReadKey();


结论:

//if有可能一条语句都不执行

//if  -else 有条件的执行一条语句,至少执行一条语句


3、循环结构:While,do-while,for 


先来看While循环:
Int i=0;While(i<100){Console.writeline(“小杨很帅啊”);i++;}Console.writeline(“小杨很帅啊”);
while向for的转换:

For(int i=0;i<100;i++){Console.writeline(小杨很帅啊);}Console.writeline(“小杨很帅啊”);

While 循环   ---先执行一次循环体   然后再判断条件  是否成立

结论:
//一般知道了循环的次数,用For循环比较方便;

//不知道循环多少次,就用while或者do-while,这时候一定要先看看是先判断还是先执行,如果先执行,用do-while,如果先判断则用while


4、跳转结构:Break,continue


continue使用代码举例:
int i = 0;while (i < 100){   Console.WriteLine("哈哈,又变帅了{0}", i);   continue;   i++;}   Console.WriteLine("");   Console.ReadKey();

break使用代码举例:
//在while中用break实现要求用户一直输入用户名和密码//只要不是admin、123一直提示要求重新输入,如果正确提示登录成功 while (true)  {    Console.WriteLine("输入帐号");    string name = Console.ReadLine();    Console.WriteLine("输入密码");    string pwd = Console.ReadLine();      if (name == "admin" && pwd == "123")         {         Console.WriteLine("登陆成功");         break;        }  }    Console.ReadKey();

两者区别:

Continue  回到条件,打断后,接着做
Break   直接跳出循环,打断后不做了

二、枚举与结构:

枚举用法:(写在命名空间里)

Public enum gender{男,女}Gender gender=Gender.男;Int number=(int)gender;   //强转

使用枚举的好处:
可以强转为int类型;
更加方便,Gender.后有枚举值;
 
Gender g=(Gender)2;Console.writeline(g);Console.readkey();Gender g=(Gender)(Enum.Parse(typeof(Gender),console.readline()));  //把字符串转化成枚举类型的值

结构声明:

如果需要显示150个人的姓名,年龄,住址等等信息,程序员需要一个一个的输,代码之多难以想象,要解决一次性声明多个变量就需要用到结构体了:

//通过结构体解决该问题  public struct Person{   public string name;  //字段   public为访问符} Person zsPerson;zsPerson._name="张三";Console.writeline(zsPerson._name);Console.readkey();


结构声明语法:
[访问修饰符]  struct 结构名
{
  结构体
}
枚举的声明方法:
[访问修饰符] enum 枚举名
{
   值1,
   值2
}

三、总结:

C#学习到现在,对他的基本知识了解已经很多了,他与VB 的共性当然是有的,但是相信,他有他的优势,VB有VB的好处,之前有了VB的基础,觉得学习C#还是能产生共鸣的,C#学习正在进行中,有什么理解不到的,还请大家指出,小编感激不尽!



0 0
原创粉丝点击