zoukankan      html  css  js  c++  java
  • 复习:学习笔记

    花了将近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,…..值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 方法名([参数]);

    {

    }

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 插入竖线
    {
        class Program
        {
            static void Main(string[] args)
            {
                string[] names = { "张三","李四","王五","赵六"};
                string str = ConInsert(names);
                Console.WriteLine(str);
                Console.ReadKey();
    
            }
            public static string ConInsert(string[]names)
            {
                string result = "";
                for (int i = 0; i < names.Length; i++)
                {
                    if (i == names.Length - 1)
                    {
                        result += names[i];                 
                    }
                    else
                    {
                        result += names[i]+"|";                    
                    }
                } 
                return result;
            }
        }
    }
    View Code

    Return:

    运行到return时,立即退出本方法.

     

    变量的作用域:

    在方法中定义的变量叫局部变量;

    {}变量的作用值在其所在的大括号{}中有用.

    *****参数: 方法名([参数])*****

    (out传入和ref传出)的使用

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    
    namespace 模拟tryparse
    {
        class Program
        {
            static void Main(string[] args)
            {
                string s = "123";
                int re;
                if (int.TryParse(s, out re))
                {
                    Console.WriteLine("转换成功,转换后的数是:" + re);
                }
                else 
                {
                    Console.WriteLine("转换失败,请检查后重新转换.");
                }
                Console.ReadKey();
            }
            static bool MyTryParse(string s,out int result)
            {
                try
                {
                    result = Convert.ToInt32(s);
                    return true;
                }
                catch 
                {
                    result = 0;
                    return false;
                }
            }
        }
    }
    View Code

    在方法名后面的括号内定义变量,叫做参数,定义的变量用于调用

    传过来的数据.

    如果一个方法一旦有参数,那么调用者必须传参数,且,传参数的类型

    必须与调用的类型一致.

    int 变量名=int.Parse(字符串变量)----把字符串转换成int

    类型,也可以用Convert.ToInt32();

    复习:

    方法:

    public static viod 方法名([参数])

    static:静态方法.(静态方法只能访问静态字段) 方法名首字母大写.

    调用:  类名.方法名();

     

    方法重载:

    一般在同一个类中,方法名相同,并且方法的参数的

    个数不同或者对应位置上的类型不同.可以构成重载.

    注意:方法重载和返回值没有关系.

     

    (1)

    Out传出数值;在方法的参数类型前面加out,传参数的时候,也必须在需要调用参数的变量前面加out,

    表明这个参数不是传入的,而是用来传出值的.

    (2) 如果参数是以out形式传入的,那么在传入前可以不赋初值.

    (3)在方法中,对于由out修饰的参数,必须对其赋值.并且必须在使用前赋值.

     

    三种转换:

    int.parse();只用于转换.

    int.TryParse();试着转换.

    convert.ToInt32();

  • 相关阅读:
    zbb20181207 springboot @ConfigurationProperties使用
    zbb20181206 logback,lombok 默认日志logback配置解析
    Spring Boot (8) 全局异常处理
    Spring Boot (7) JdbcTemplate访问数据库
    Spring Boot (6) Spring Data JPA
    Spring Boot (4) 静态页面和Thymeleaf模板
    Spring Boot (3) 热部署devtools
    Spring Boot (2) Restful风格接口
    Spring Boot (1) 构建第一个Spring Boot工程
    idea使用maven搭建ssm框架实现登陆商品增删改查
  • 原文地址:https://www.cnblogs.com/jerryho/p/3638769.html
Copyright © 2011-2022 走看看