首页 > 学院 > 开发设计 > 正文

复习:学习笔记

2019-11-17 03:15:28
字体:
来源:转载
供稿:网友

复习:学习笔记

花了将近20天终于把第一季剩下的学完了,越到后面感觉好多不懂,都不知道如何着手写程序,一定要多练多思考,虽然感觉很难,但是不能放弃.

整理下学习的笔记,现在想想过的还是很充实的,虽然学习很忙,但是课外时间全都用在自学C#上了,接下来还有很长的路要走,还得继续努力才行.

break:

(1)用于switch-case判断中,用于跳出switch.

(2)用于跳出当前循环.

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace break_continue{    class PRogram    {        static void Main(string[] args)        {            bool flag = true;            int age = 0;            int sum = 0;            for (int i = 0; i < 5; i++)            {                Console.WriteLine("请输入{0}个人的年龄?",i+1);                try                {                    age = Convert.ToInt32(Console.ReadLine());                    //sum += age;                    if (age < 0 || age > 100)                    {                        Console.WriteLine("你输入的年龄非法.");                        flag = false;                        break;                    }                    sum += age;                                   }                catch                {                    Console.WriteLine("你输入的年龄不是数字,请重新运行程序输入.");                    flag = false;                    break;                                   }                                               }            if (flag == true)            {                Console.WriteLine("这五个人的平均年龄是{0}", sum / 5);            }            Console.ReadKey();        }    }}
View Code

Continue:

用于循环中,程序一旦执行到continue语句,立即结束本次循环(就是不再执行循环体中continue下面的语句了),直接进行下一次循环.

(do-while直接进行下一次循环条件的判断,如果条件成立,)

三元表达式:

表达式1?表达式2:表达式3;

首先计算表达式1(求解成一个bool类型的值)

如果表达式1的值为true,则表达式2的值作为整个表达式的值,

如果表达式1为false,则表达式2作为整个表达式的值.

枚举/常量/结构:

常量: const 类型 常量名=常量值

在定义时赋值,其他地方不允许改变其值.

枚举:(定义枚举时,不能是int类型)

定义一种枚举类型,并且在定义这种类型时,指定这个类型的所有的值

语法: enum 类型名称{值1,值2,&hellip;..值n}

(一般和类的定义在同一个级别,这样在同一个命名空间下所有的类都可以使用这个枚举)

枚举的作用(1)限制用户不能随意赋值,只能在定义的枚举中的值选择

(2)不需要死记每一个值,只需要选择相应的值即可.枚举的类型的变量都可以强制转换成一个int类型.

把字符串转化成枚举类型:

(自枚)(Enum.Parse(typeof(自枚),”待转换的字符串”))

结构语法(也是一种类型): 访问修饰符 struct 结构名

(为什么要用结构)?

{

定义结构成员

}

定义好结构后,可以直接声明相应的变量

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 结构{   enum Gender//枚举 定义了一个 Gender的类   {       男,       女   }    public struct Person //定义了一个 结构名 为 Person的类    {        public string name;        public int age;        public string sex;    }    class Program    {        static void Main(string[] args)        {            Gender sex;            sex = Gender.男;            switch (sex)            {                case Gender.男:                    Console.WriteLine("男性");                    break;                case Gender.女:                    Console.WriteLine("女性");                    break;                   default:                    break;            }                        Person firstPenson,secondPenson;            firstPenson.name = "小兰";            firstPenson.sex = "女";            firstPenson.age = 18;                Console.WriteLine("{0}今年{1}岁,性别是{2}.",firstPenson.name,firstPenson.age,firstPenson.sex);                       secondPenson.name = "张三";            secondPenson.sex = "男";            secondPenson.age = 20;            Console.WriteLine("{0}今年{1}岁,性别是{2}.", secondPenson.name, secondPenson.age, secondPenson.sex);                Console.ReadKey();        }    }}
View Code

数组:一次声明多个相同类型的变量.这些变量在内存中是连续存储的.(求平均值,最大值求和,排序 )

语法:

数据类型[] 数组名=new 数据类型[数组长度];

数组长度:申明几个变量长度就是多少,

例如5个人的成绩,则长度为5

int[] score=new int[5] s声明了一个数组,里面包含5个

int类型的变量,数组名叫:score.

访问数组:数组名[]=值 score[0]=10;

int类型数组一旦声明,里面的每一个元素背初始化为0.

通过 数组名.Length 可以获取数组长度

using System;using System.Collections.Generic;using System.Linq;using System.Text;namespace 数组{    class Program    {        static void Main(string[] args)        {            int max;            int sum = 0;            int[] score = new int[10]; //定义了一个int类型,长度为10的数组.            for (int i = 0; i < score.Length; i++)            {                //对数组中的值赋值                Console.WriteLine("请输入第{0}个学生的成绩", i + 1);                score[i] = Convert.ToInt32(Console.ReadLine());                //  sum+=score[i];            }            //求数组中的最大值.            max = score[0];//先假定最大值为数组中的第一个值.            for (int i = 1; i < score.Length; i++)            {                if (score[i] > max)                {                    max = score[i];                }            }            Console.Clear();            //通过一个循环求数组的所有值的和            for (int i = 0; i < score.Length; i++)            {                sum += score[i];            }            Console.WriteLine("{0}个人平均成绩是{1}", score.Length, sum / score.Length);            //通过一个循环显示所有人的成绩            for (int i = 0; i < score.Length; i++)            {                Console.WriteLine("第{0}个人的成绩是{1}", i + 1, score[i]);            }            Console.WriteLine("数组中最高分为:{0}", max);            Console.ReadKey();        }    }}
View Code

Console.Clear(); 用于清屏.

方法(函数):与Main方法同等级(在同一个括号里)

[访问修饰符][static] 返回值类型方法名([参数])

[ ]中的可写可不写.

{

方法体;

}

(1)方法一般要定义在类中.

(2)如果方法没有返回值,我们就写void.

(3)如果方法没有参数,小括号()不能省略.

如果是静态方法(由static修饰) 则使用 类名.方法名();

在类中调用本调的方法可以直接写方法名();

public static void 方法名([参数]);

{

}

发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表