C#的学习记录1

第一天

//控制台输出语句
Console.WriteLine("hello world"); 
//控制台输入的数据,等待控制台输入数据
Console.ReadLine("");
//控制控制台保存时间,简单说就是读取数据等待,下一步操作
Console.ReadKey();

第二天

1、代码折叠注释:

#region
#endregion

2、变量的输出方式 (+ {} $ )

String name = '张三';
int age = 12;
char gender = '男';
//将变量输出打印
//方式一:
Console.WrirteLine("我的名字叫"+name+"今年"+age+"了,我是的一个"+gender+"生");
//方式二:
Console.WriteLine("我的名字叫{0},今年{1}了,我是一个{2}生",name,age,gender);
//方式三:
Console.WriteLine($"我的名字是{name},今年{age}了,我是一个{gender}生")

3、快捷键

ctrl +k+d 格式化代码(整理代码)

ctrl + k+c 注释

ctrl +k + u 取消注释

4、命名规则

  1. Pascal Case(帕斯卡式命名):
    • 类名、接口名和枚举类型名应该使用帕斯卡式命名。
    • 示例:public class MyClass, public interface IMyInterface, public enum MyEnum
  2. Camel Case(驼峰式命名):
    • 变量名、方法名和参数名一般使用驼峰式命名。
    • 示例:int myVariable, void myMethod(), void processRequest(int parameter)
  3. 全大写字母:
    • 常量名通常使用全大写字母,并用下划线 _ 分隔单词。
    • 示例:public const int MAX_COUNT = 100;
  4. 匈牙利命名法:
    • 不推荐使用匈牙利命名法(例如在变量名前加上类型前缀),因为它已经被更现代的命名约定所取代。
  5. 命名约定:
    • 命名应该清晰、具有描述性,能够准确反映其用途和含义。
    • 避免使用单个字符命名(除非是临时变量或循环变量)。
    • 使用英文单词或约定俗成的缩写,确保名称可读性。
  6. 私有字段:
    • 私有字段通常以 _ 开头,后面使用驼峰式命名。
    • 示例:private int _myPrivateField;

第三天

1、练习题一

//在控制台输入数据,再统一输出
//让用户输入姓名 语文 数学 英语 三门课的成绩,然后给用户显示:XX,你的总成绩为XX分,平均成绩为XX分。
//1、分别的提示用户输入姓名、语文、数学、英语
//2、类型转换  Convert
Console.WriteLine("请输入您的名字");
String name = Console.ReadLine();
Console.WriteLine("请输入语文成绩");
int chine = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入数学成绩");
int math = Convert.ToInt32(Console.ReadLine());
Console.WriteLine("请输入英语成绩");
int english = Convert.ToInt32(Console.ReadLine());
//第二天三种输出方式都可以
Console.WriteLine($"我叫{name},语文考了{chine},数学考了{math},英语考了{english}");
Console.ReadKey();

2、运算符

  • 算数运算符:+ - * / % ,二元算数运算符,至少需要两个操作数参与运算
  • 算数运算符:++ --,一元算数运算符,只需要一个操作数即可完成运算

如果在一个表达式中,既有一元运算符,又有二元运算符,先算一元运算符

一元运算符的优先级要高于二元运算符

3、前++和后++的区别

  • 不管你是前++还是后++,最终你的结果,都会+1

  • 如果是后++,会拿原值先进行参与运算,运算完成后,自动+1

  • 如果是前++,会先把值自动+1,然后拿着+1后的值,去参与运算

    int a = 7;
    int b = a++ + ++a * 2 + --a + a-- * 2 + ++a;
    // 7 + 9 * 2 + 8 + 8 *2 + 8 = 57
    Console.WriteLine(a);
    Console.WriteLine(b);
    Console.ReadKey();

4、关系运算符 < >

//大象的重量(1500)>老鼠的重量(1)
//兔子的寿命(3) > 乌龟的寿命(1000)
//39 < 18
//我的年龄(20) == 你的年龄(20)
//比大比小的结果是什么 ?

bool a = 1500 > 1;

bool b = 39 < 18;
Console.WriteLine(a);
Console.WriteLine(b);
Console.ReadKey();

5、逻辑运算符 & | && ||

练习二 :求闰年

//二、润年的判定(符合下面两个条件之一):
//年份能够被400整除.(2000)
//年份能够被4整除但不能被100整除.(2008)
//让用户输入一个年份,如果是润年,则输出true,如果不是,则输出false.
//2100 / 1600 / 1800 / 2009年是闰年吗?
Console.WriteLine("请输入一个年份");
int year = Convert.ToInt32(Console.ReadLine());
bool a = year % 400 == 0 || (year % 4 == 0 && year % 100 != 0);

Console.WriteLine(a);
Console.ReadKey();

6、赋值运算符 += -= *= /= %/

 int number = 10;
 number *= 2;  //number = number *2;
 number -= 1;  //19
 number /= 2;  //除数   9
 number %= 2;  //取余数 1
 number += 2;  // 3
 Console.WriteLine(number);
 Console.ReadKey();

7、if语句

- if 语句

    //编程实现:如果跪键盘的时间大于60分钟,那么媳妇奖励我晚饭不用做了.
    Console.WriteLine("您的跪了多久了");
    int a = Convert.ToInt32(Console.ReadLine());
    
    
    if (a>60) {
        Console.WriteLine("我晚饭不用做了");
    }
    else{
        Console.WriteLine("继续给我跪着");
    }
    Console.ReadKey();

- if-esle

     //2、 要求用户输入两个数a、b,如果a和b整除或者a加b大于100,则输出a的值,否则输出b的值。
    
     Console.WriteLine("请您输入两个数字,这是第一个");
     int a = Convert.ToInt32(Console.ReadLine());
     Console.WriteLine("请您输如两个数字,这是第二个");
     int b = Convert.ToInt32(Console.ReadLine());
     if (a / b == 0 || a + b > 100)
     {
         Console.WriteLine(a);
     }
     else {
         Console.WriteLine(b);
     }
    
     Console.ReadKey();

- if-esle-if

    //练习3:提示用户输入年龄,如果大于等于18,则告知用户可以查看,如果小于10岁,
    //则告知不允许查看,如果大于等于10岁并且小于18,则提示用户是否继续查看(yes、no),
    //如果输入的是yes则提示用户请查看,否则提示"退出,你放弃查看"。
    
    Console.WriteLine("请用户输入年龄");
    int age = Convert.ToInt32(Console.ReadLine());
    if (age >= 18) {
        Console.WriteLine("该用户可以查看");
    } else if (age < 10) {
        Console.WriteLine("你太小了");
    } else  {
        Console.WriteLine("用户是否继续查看(yes、no)");
        String pan = Console.ReadLine();
        if (pan == "yes") {
            Console.WriteLine("啦啦啦啦啦啦");
        }
    }
    Console.ReadKey();

8、try-catch

     try
     {
         int a = 0;
         int b = 0;
         int c = b / a;
     }
     catch {
         Console.WriteLine("啦啦啦");
     }
     Console.ReadKey();

9、循环结构

    //循环结构:当满足某个条件或者某些条件的时候,不停的去做一些事情。
    //死循环:永远不会停止的循环
    
    //1、打印100次"欢迎跟老赵学习技术"
    //循环体:打印 。。。。。。。。
    //循环条件 没有打印到100次
    //int a = 0;
    //while (a < 100) {
    //    Console.WriteLine("欢迎跟老赵学习技术");
    //    a++;
    //}
    
    
    
    
    //2、输入班级人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩
    //循环体:输入学员成绩
    //循环条件:输入的次数,小于等于班级人数
    
    //Console.WriteLine("输入班级人数");
    //int sum = Convert.ToInt32(Console.ReadLine());
    //int reg = 0;
    //int scoreSum = 0;
    //while (reg < sum) {
    //    Console.WriteLine("请输入第{0}成绩",reg+1);
    //    int score = Convert.ToInt32(Console.ReadLine());
    //    scoreSum += score;//累加
    //    reg++;
    //}
    //Console.WriteLine("班级的平均成绩是{0},总成绩是{1}",scoreSum/reg,scoreSum);
    
    
    //3、老师问学生,这道题你会做了吗? 如果学生答"会了(y)",则可以放学.如果学生不会做(n),则老师再讲一遍,再问学生是否会做了......直到学生会为止,才可以放学.
    // 直到学生会或老师给他讲了10遍还不会,都要放学
    //循环体:老师不停的问 学生不停的答
    //循环条件:我不会  没有讲够10遍
    
    string answer = "no";
    int i = 0;//记录循环的次数
    while (i < 10 && answer == "no")
    {
        Console.WriteLine("你会了吗?这是我第{0}遍给你讲", i + 1);
        answer = Console.ReadLine();
        i++;
    }
    Console.ReadKey();

10、变量作用域

在C#中,变量的作用域决定了它们在程序中可见和可访问的范围。C#中的变量可以分为以下几种作用域:

1. 局部变量(Local Variables):
   - 局部变量是在方法、构造函数或任何代码块(如 if、for、while等)内部声明的变量。
   - 它们的作用域仅限于声明它们的代码块内部。
   - 示例:
         void MyMethod()
         {
             int localVar = 10;
             // localVar 只能在 MyMethod 方法内部访问
         }
2. 形式参数(Parameters):
   - 形式参数是方法签名中声明的变量,用于接收调用方法时传入的值。
   - 它们的作用域仅限于方法体内部。
   - 示例:
         void MyMethod(int parameter)
         {
             // parameter 是 MyMethod 的参数,在方法内部可访问
         }
3. 成员变量(Member Variables):
   - 成员变量是类或结构体中声明的变量,在整个类或结构体中可见和访问。
   - 它们的作用域是整个类或结构体的定义范围。
   - 示例:
         public class MyClass
         {
             private int memberVariable;
         
             public void MyMethod()
             {
                 // 可以在 MyClass 中的任何方法中访问 memberVariable
             }
         }
4. 静态变量(Static Variables):
   - 静态变量是使用 static 关键字声明的类成员变量,它们属于类而不是类的实例。
   - 它们的作用域是整个类的定义范围,无需创建类的实例即可访问。
   - 示例:
         public class MyClass
         {
             private static int staticVariable;
         
             public static void MyStaticMethod()
             {
                 // 可以在 MyClass 的静态方法中直接访问 staticVariable
             }
         }
5. 全局变量(Global Variables):
   - 在C#中,全局变量的概念不像一些其他编程语言那样明确定义。一般来说,全局变量指的是在命名空间层面定义的变量,对整个命名空间可见。
   - C#中没有直接的全局变量概念,但可以通过静态类和静态成员来实现类似的全局可访问性。

作用域规定了变量的可见性和生命周期,帮助程序员有效地管理变量的使用和内存分配。

## 第四天

1、枚举

    public enum Sex { 
        男,
        女
    }
    
    public enum WeChatState
    {
        happy,
        伤心,
        发呆,
        忙碌
    }
     static void Main(string[] args)
     {
         Sex a = Sex.男;
         
         WeChatState w1 = WeChatState.发呆;
         int n1 = (int)w1;//强制类型转换  枚举跟整数是兼容的
         //Console.WriteLine(n1);
         Console.WriteLine((int)WeChatState.happy);
         Console.WriteLine((int)WeChatState.伤心);
         Console.WriteLine((int)WeChatState.发呆);
         Console.WriteLine((int)WeChatState.忙碌);
    
         //WeChatState w1 = WeChatState.忙碌;
         //int n1 = (int)w1;
         Console.WriteLine(n1);
    
         //WeChatState weChatState = (WeChatState)0;
         WeChatState weChatState1 = (WeChatState)1;
         Console.WriteLine(weChatState1);
         Console.ReadKey();
     }

2、练习一

    //1、打印100次"欢迎跟老赵学习技术"
    for (int i = 0; i < 100; i++)
    {
        Console.WriteLine("欢迎跟老赵学习技术");
    }
    Console.ReadKey();
    
    
    //2、输入班级人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩
    
    int sum = 0;
    double avg = 0;
    Console.WriteLine("请输入班级人数");
    int number = Convert.ToInt32(Console.ReadLine());
    for (int a = 0; a < number; a++)
    {
        Console.WriteLine("请输入第{0}个学生的成绩", a + 1);
        sum += Convert.ToInt32(Console.ReadLine());
    }
    avg = sum / number;
    Console.WriteLine("班级总分是" + sum);
    Console.WriteLine("班级平时分是" + avg);
    Console.ReadKey();
    
    
    //3、老师问学生,这道题你会做了吗? 如果学生答"会了(y)",则可以放学.如果学生不会做(n),则老师再讲一遍,再问学生是否会做了......直到学生会为止,才可以放学.
     //直到学生会或老师给他讲了10遍还不会,都要放学
    
    
    int i = 0;
    for (i = 1; i <= 10; i++)
    {
        Console.WriteLine("这是第{0}次提问你", i);
        string answer = Console.ReadLine();
        if (answer != "y")
        {
            continue;
        }
        else if (answer == "y")
        {
            Console.WriteLine("学生在第{0}次学会了", i);
            break;
        }
    }
    if (i > 10)
    {
        Console.WriteLine("十次都不会,废了");
    }
    Console.ReadKey();
    
    //4、2006年培养学员80000人,每年增长25 %,请问按此增长速度,到哪一年培训学员人数将达到20万人?
    
    int year = 2006;
    for (double i = 80000; i <= 200000; i *= 1.25)
    {
        year++;
    }
    Console.WriteLine(year);
    Console.ReadKey();
    
    
    //5、要求用户只能输入yes或者no,否则就一值重新输入。
    
    while (true)
    {
        Console.WriteLine("请输入yes或no");
        string str = Console.ReadLine();
        if (str == "yes" || str == "no")
        {
            Console.WriteLine("跳出成功");
            break;
        }
    
    }
    Console.ReadKey();
    
    
    
    //6、用户输入用户名和密码,三次都错误的话,跳出。
    
    int i = 0;
    string name = "888888";
    string pwd = "666666";
    for (i = 1; i <= 3; i++)
    {
        Console.WriteLine("请输入用户名");
        string idName = Console.ReadLine();
        Console.WriteLine("请输入密码");
        string idPwd = Console.ReadLine();
        if (idName == name && idPwd == pwd)
        {
            Console.WriteLine("登录成功");
            break;
        }
        else
        {
            Console.WriteLine("登录失败");
        }
    }
    if (i > 3)
    {
        Console.WriteLine("锁死");
    }
    Console.ReadKey();

3、while与break

    static void Main(string[] args)
    {
        #region 练习一
        //练习1:循环录入5个人的年龄并计算平均年龄,如果录入的数据出现负数或大于100的数,
        //立即停止输入并报错.
        //try
        //{
        //    for (int a = 0; a < 5; a++)
        //    {
        //        Console.WriteLine("请输入第{0}个人的年龄", a + 1);
        //        int age = Convert.ToInt32(Console.ReadLine());
        //        if (age <= 0 || age > 100)
        //        {
        //            int b = 0;
        //            b = b / 0;
        //        }
        //    }
        //}
        //catch (Exception)
        //{
        //    Console.WriteLine("没有这个年龄的");
        //    throw;
        //}
        //Console.ReadKey();
    
        #endregion
    
        #region 练习二
        //练习2:在while中用break实现要求用户一直输入用户名和密码,
        //只要不是admin、88888就一直提示要求重新输入,如果正确则提登录成功.
        //while (true) {
        //    Console.WriteLine("请输入账户名");
        //    String uname = Convert.ToString(Console.ReadLine());
        //    Console.WriteLine("请输入密码");
        //    String psw = Convert.ToString(Console.ReadLine());
    
        //    if (uname =="admin" && psw=="88888") {
        //        Console.WriteLine("登陆成功");
        //        break;
        //    }
        //}
        //Console.WriteLine("哦啦哦啦哦啦");
        //Console.ReadKey();
    
        #endregion
    
    
        // 练习3:1~100之间的整数相加,得到累加值大于20的当前数(比如: 1 + 2 + 3 + 4 + 5 + 6 = 21)结果6
        int sum = 0;
        int i = 1;
        while (i<=100) {
          
            sum += i;
            
            if (sum >20) {
                Console.WriteLine("结果为"+i);
    
                break;
            }
            i++;
        }
        Console.ReadKey();
    }

4、for循环

    //问题3: 输出九九乘法表(循环的嵌套)
    //问题1:求1-100间的所有偶数和    /奇数和/整数和?
    
    for (int i =1;i<=9;i++) {
        for (int j = 1; j <= i; j++) {
            Console.Write("{0}*{1}="+i*j+" ",i,j);
        }
        Console.WriteLine();
    }
    Console.ReadKey();

5、do_while

    //明天小兰就要登台演出了,老师说再把明天的演出的歌曲唱一遍,如果满意,小兰就可以回家了.否则就需要再练习一遍,直到老师满意为止.(y/n)
    //while    or  do -while ?
    String answer = "";
    do {
        Console.WriteLine("老师,我唱的这一遍,你满意吗?");
         answer = Console.ReadLine();
    }
    while (answer != "yes");
    
    Console.WriteLine("放学");
    Console.ReadKey();

6、continue

    //break  跳出当前的case 当前循环
                int i = 0;
    while (i < 5)
    {
        Console.WriteLine("hahahahha");
        i++;
        continue;
        Console.WriteLine("人人二人人");
    }
    
    Console.ReadKey();
    
    
    //练习1:用 while continue实现 计算1到100(含)之间的除了能被7整除之外所有整数的和。
                int a = 1;
    int sum = 0;
    while (a <= 100)
    {
        if (a % 7 == 0)
        {
            a++;
            continue;
        }
    
        sum += a;
        a++;
    }
    Console.WriteLine("计算1到100(含)之间的除了能被7整除之外所有整数的和{0}", sum);
    
    Console.ReadKey();
    
    
    //练习2:找出100内所有的素数/质数(只能被1和自身整除的数字,是质数)1不是质数
    
    //循环/遍历  1-100所有的整数  
    //判断当前数字是否是质数     7  2-->6  --->如果没有余数,则当前数字是质数,否则不是质数

7、三元表达式

    //语法:
    //表达式1? 表达式2 :表达式3
    
    //表达式1为关系表达式,如果表达式1的值为true,则表达式2就是整个三元表达式的结果,
    //否则表达式3才是整个三元表达式的结果
    
    //三元表达式的结果类型,必须跟表达式2和表达式3的结果类型一致
    
    //提示用户输入姓名,只要不是老赵,那就是渣男
    Console.WriteLine("请用户输入姓名");
    String name= Console.ReadLine();
    if (name=="老赵"?true:false) {
        Console.WriteLine("渣男");
    }
    Console.WriteLine();
    Console.ReadKey();

8、类型转换

    //强制类型转换
    //自动类型转换
    //"123"  --> 123 Convert.ToInt32 Convert.ToDouble
    
    //int number = int.Parse("123abc");//Convert.ToInt32---->int.Parse() 把字符串转换为数字
    //Console.WriteLine(number);//WriteLine()能够把数据打印到控制台
    //Console.ReadKey();//在控制台暂停,等待用户的继续输入
    //int n = 10;
    
    //int number = int.Parse("123abc");
    //Console.WriteLine(number);
    //Console.ReadKey();
    
    TryParse:尝试性的把一个字符串,转换为整数,如果转换成功,则返回true,并将转换成功的整数,赋值给第二个参数。
    如果转换失败,则返回false,并将第二个参数的值,赋值为0;
    
    //bool b = int.TryParse("123abc", out n);
    //Console.WriteLine(n);
    //Console.WriteLine(b);
    //Console.ReadKey();
    //int n = 0;
    
    //bool b = int.TryParse("123zbc", out n);
    //Console.WriteLine(b);
    //Console.ReadKey();
    
    
    //方法/函数
    //帮助我们实现一个功能
    //我们在调用一个方法的时候,传入的数据:参数,方法执行完成后,返回的数据,我们称之为返回值。
    //参数/返回值
    
    //参数:完成一个方法,所必须要提供的条件(1个,2个,N个)
    //返回值:方法执行完成后的结果。1个。
    
    //你找你的舍友帮助你打饭,你的舍友,就是你调用的方法。
    //吃啥?饭费?  参数
    //你躺在被窝里,拿到了热乎乎的饭菜  返回值
    //功能:帮你打饭
    
    
    //Random r=new Random();
    //Int number = r.Next(1, 11); 产生1 - 10的数
    //输入名字随机显示这个人上辈是怎么死的…
    
    //面向对象  c#基础编程:基础语法    方法/函数     面向对象编程
    
    //创建了一个随机数的对象
    while (true)
    {
        //Random random = new Random();
        //int rNumber = random.Next(1, 11);
        //Console.WriteLine(rNumber);
        //Console.ReadKey();
    
        Random random = new Random();
        int rnumber = random.Next(0,100);
        Console.WriteLine(rnumber);
        Console.ReadKey();

9、常量const

    const int a = 1;
    //a = 2;
    #endregion

## 第五天

1、复习一

    internal class Program
    {
        
        public enum Seasons 
        {
            春,
            夏,
            秋,
            冬
        }
        public enum Gender { 
            男,
            女
        }
        static void Main(string[] args)
        {
            //for循环   循环次数
            //while  true  break;   ----->Continue  循环条件
    
            //三元表达式   表达式1?表达式2:表达式3  if-else
    
            //int.parse  int.TryParse 
            //常量
            //枚举
            //public 访问修饰符  修饰一个成员的访问权限
    
            int number = 100;
            Seasons seasons = Seasons.冬;
    
            //整数转换 字符串转换
            //兼容
            Console.WriteLine((int) seasons);
    
            //字符串
            //所有的数据类型,基本上都可以通过toString()转换成字符串
            string s = "春";
            Seasons s2 = (Seasons)Enum.Parse(typeof(Seasons) ,s);
            Seasons s3 = (Seasons)Enum.Parse (typeof(Seasons) ,s);
    
            Console.WriteLine(s2);
            Console.ReadKey(); 
        }
    }

2、结构体(过渡体吧)

    // 模板 人类
    public struct People
    {
        //字段
        public string name;
        public int age;
        public Gender gender;
    }
    
    public enum Gender { 
        男,
        女
    }
    internal class Program
    {
        static void Main(string[] args)
        {
            //面向对象 ————》C语言
            People zhangsan;
            zhangsan.age= 5;
            zhangsan.gender = Gender.男;
            zhangsan.name = "张三";
    
            People lisi = new People();
            lisi.name = "里斯";
            lisi.age = 15;
            lisi.gender = Gender.男;
    
            Console.WriteLine("名字是{0},年龄是{1},性别{2}",zhangsan.name,zhangsan.age,zhangsan.gender);
            Console.WriteLine("名字是{0},年龄是{1},性别{2}", lisi.name, lisi.age, lisi.gender);
            Console.ReadKey();
        }
    }

    //2 定义一个结构类型Person,有三个成员,分别为姓名,性别,年龄
    //声明两个Person类型的变量,分别表示 张三 男  18岁 / 小兰 女 16岁
    Person zhangsan = new Person();
    zhangsan.name = "张三";
    zhangsan.age = 12;
    zhangsan.gender = Gender.男;
    
    Person xiaolan = new Person();
    xiaolan.name = "小兰";
    xiaolan.age = 22;
    xiaolan.gender = Gender.女;
    
    Console.WriteLine(zhangsan.name);
    Console.WriteLine(xiaolan.name);
    
    Console.ReadKey();

3、数组

    //场景:一次语文测试后,老师让班长统计每一个学生的成绩并计算全班
    //(全班共60人)的平均成绩,然后把所有成绩显示出来.
    
    //1、现有一个数组
    //语法:数组类型[] 数组名 = {值.........};
    Random random = new Random();//表示创建一个随机数的对象
    //int[] nums = new int[10];
    //int[] numbers = { 1, 2, 3, 4, 5 };
    
    //int[] numbers1 = new int[5]; //创建数组对象,确定了数组的长度,也没有给赋值,默认的初值的就是0
    //bool[] bools = new bool[5];
    //Console.WriteLine(numbers[0]);
    //Console.WriteLine(numbers[1]);
    //Console.WriteLine(numbers[2]);
    //Console.WriteLine(numbers[3]);
    //Console.WriteLine(bools[4]);
    
    
    //Console.ReadKey();
    
    //确实了数组的长度,不给赋初值
    int[] numbers =new int[10];
    numbers [0] = 1;
    numbers [1] = 2;//给数组的下标赋值,最大值就是数组的长度-1
    
    //在给数组赋值的时候,通过循环进行赋值
    
    for (int i = 0; i < numbers.Length; i++) {
        numbers[i] = i;
    }
    
    for (int i = 0; i < numbers.Length; i++) { 
        Console.WriteLine(numbers[i]);
    }
    
    Console.ReadKey();

数组的练习

    //练习1:从一个整数数组中取出最大的整数,最小整数,总和,平均值
    //int[] numbers = { 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    要保证给最大值和最小值的初值,都是数组中的元素
    
    //int max = numbers[0];
    //int min = numbers[0];
    
    //int sum = 0;
    //int avg = 0;
    
    //for (int i = 1; i < numbers.Length; i++) {
    //    if (numbers[i] >max) {
    //        max= numbers[i];
    //    }
    //    if (numbers[i] < min) { 
    //        min= numbers[i];
    //    }
    //    sum += numbers[i];
    //}
    
    //avg= sum / numbers.Length;
    
    //Console.WriteLine(avg);
    //Console.WriteLine(sum);
    //Console.ReadKey();
    
    
    //练习3:数组里面都是人的名字,分割成:例如:老赵|老班|老邹…”最后一个名字后没有|线
    //String[] arr = { "老赵", "老班", "老邹" };
    //String a = "";
    //for (int i = 0; i < arr.Length - 1; i++) {
    //    a  += arr[i]+"|";
    // }
    //a = a + arr[arr.Length-1];
    
    //Console.WriteLine(a);
    //Console.ReadKey();
    
    //练习4:将一个整数数组的每一个元素进行如下的处理:
    //如果元素是正数则将这个位置的元素的值加1,
    ///如果元素是负数则将这个位置的元素的值减1,如果元素是0,则不变。
    
    //int[] numbers = { -1, -2, -3, 0, 1, 2, 3 };
    //for (int i = 0; i < numbers.Length; i++)
    //{
    //    if (numbers[i] > 0)
    //    {
    //        numbers[i]++;
    //    }
    //    if (numbers[i] < 0) {
    //        numbers[i]--;
    //    }
    //}
    
    //for (int i = 0; i < numbers.Length; i++)
    //{
    //    Console.WriteLine(numbers[i]);
    //}
    //Console.ReadKey();
    
    //练习5:将一个字符串数组的元素的顺序进行反转。{“我”,“是”,”好人”}
    //{“好人”,”是”,”我”}。第i个和第length - i - 1个进行交换。	
    
    string[] names = { "我", "是", "好人" };   //"吗" "好人" "是" "我"
    
    //确定循环次数
    for (int i = 0; i < names.Length / 2; i++)
    {
        String temp = names[i];
        names[i] = names[names.Length - 1 - i];
        names[names.Length - 1 - i] = temp;
    }
    
    for (int i = 0; i < names.Length; i++)
    {
        Console.WriteLine(names[i]);
    }
    Console.ReadKey();

数组的赋值

    static void Main(string[] args)
    {
        int[] numbers = { 1, 2, 3, 4, 5 };
        int[] numbers2 = { 6, 7, 8, 9, 10, 11, 12, 13, 14 };
    
        numbers = numbers2;
    
        numbers2[0] = 100;
    
        for (int i = 0; i < numbers.Length; i++)
        {
            Console.WriteLine(numbers[i]);
        }
        Console.ReadKey();
    }



4、冒泡排序

    //冒泡排序
    int[] numbers = { 9, 8, 7, 6, 5, 4, 3, 2, 1, 0 };
    
    for (int i = 0; i < numbers.Length-1; i++) {
        for (int j = 0; j < numbers.Length-i-1; j++) { 
            int temp = numbers[j+1];
            numbers[j+1] = numbers[j];
            numbers[j] = temp;
    
        }
    
    }
    for (int a = 0; a < numbers.Length; a++)
    {
        Console.WriteLine(numbers[a]);
    }
    
    Console.ReadKey();

5、对象

    static void Main(string[] args)
    {
        //函数/方法就是将一堆代码进行重用的一种机制
    
        //王者荣耀
        Console.WriteLine("开局,先选一个英雄");
        Console.WriteLine("凑人数,等待游戏开始");
        Console.WriteLine("游戏开始啦!!!!!!!");
    
        PlayGame();
    
        Console.WriteLine("推塔、杀人");
        Console.WriteLine("被秒了");
    
        PlayGame();
    
        Console.WriteLine("跳到了敌人的面前,准备正面PK");
        Console.WriteLine("被秒了");
    
        PlayGame();
    
        Console.ReadKey();
    }
    
    static void PlayGame()
    {
        Console.WriteLine("偷偷的狗着,去没人的地方打怪");
        Console.WriteLine("赚钱、升级、买装备");
    }

方法的练习

    static void Main(string[] args)
    {
        //写一个方法,来实现:求 两个整数之间的最大值
        //类名省略  实参的名字跟形参的名字,没有关系。要符合形参的个数和类型
        int s1 = 100;
        int s2 = 200;
        int a=  GetMax(1,2);
    
        Console.WriteLine(a);
        Console.ReadKey();
    }
    
    public static int GetMax(int a, int b) {
        if (a>b) {
            return a;
        }
        return b;
    }

    static void Main(string[] args)
    {
        //1 读取输入的整数,定义成方法,多次调用(如果用户输入的是数字, 则返回, 否则提示用户重新输入)
        //2 还记得学循环时做的那道题吗? 只允许用户输入y或n, 请改成方法
        //3查找两个整数中的最大值:int Max(int i1, int i2)
        //4计算输入数组的和:int Sum(int[] values)
    
        //1 读取输入的整数,定义成方法,多次调用(如果用户输入的是数字, 则返回, 否则提示用户重新输入)
        //  ------>  功能     参数    返回值
    
        //2 还记得学循环时做的那道题吗? 只允许用户输入y或n, 请改成方法
        while (true) {
            Console.WriteLine("请输入y或者n");
            string str = Console.ReadLine();
            bool b = GetYorN(str);
            if (b)
            {
                break;
            }
        }
    }
    public static bool GetYorN(string input)
    {
        if (input == "y" || input == "n")
        {
            return true;
        }
        else
        {
            return false;
        }
    }

6、return返回值

     static void Main(string[] args)
     {
         while (true) {
             Console.WriteLine("哈哈哈哈");
         return;
         }
    
         Console.WriteLine("l咯咯咯咯咯");
         Console.ReadKey();
     }	

## 第六天

1、局部变量和全局变量

    //静态变量
    static int a = 1;
    static void Main(string[] args)
    {
        int a = 2;
        get(a);
        Console.WriteLine(a);
        Console.ReadLine();
        Console.ReadKey();
    
    }
    
    public static void get(int a) {
        a += 8;
        Console.WriteLine(a);
    }

2、方法的重载

        //方法重载,就是一个方法中可以传入不同个数/类型的参数
        //重载:方法的名称相同,参数不同(1、类型不同  2、个数不同)
        int a = 1;
        int b = 2;
        String s = "我";
        Test();
        Test(a);
    
        Test(a,b);
        Test(s);
        Test();
    
        Console.ReadKey();
    }
    public static void Test() {
        Console.WriteLine("Test的无参方法");
    }
    public static void Test(int a) {
        Console.WriteLine("Test的一个int类型参数的方法");
    }
    
    public static void Test(int a,int b)
    {
        Console.WriteLine("Test的二个int类型参数的方法");
    }
    
    public static void Test(String a)
    {
        Console.WriteLine("Test的y一个String类型参数的方法");
    }

3、out参数

    static void Main(string[] args)
    {
        //out一般用在函数需要有多个返回值的场所。
        //练习1:
        //写一个函数 来判断用户登录是否成功
        //如果成功 则返回true,还要返回一条登录信息(登录成功)
        //如果失败 则返回false ,告诉用户到底哪错了
        bool a =true;
        String msg;
        while (a) {
            Console.WriteLine("请输入您的用户名");
            String name = Console.ReadLine();
            Console.WriteLine("请输入您的密码");
            int pwd = int.Parse(Console.ReadLine());
            bool b = IsLogin(name,pwd, out msg);
            if (b) {
                Console.WriteLine("登陆结果{0},登陆信息{1}", b, msg);
                a = false;
            }
    
        }
        Console.ReadKey();
    }
    
    private static bool IsLogin(string name, int pwd, out string msg)
    {
        bool b = false;
        if (name == "admin" && pwd ==123) {
    
            msg = "登陆成功";
            return true;
        }
        else if (name == "admin")
        {
            msg = "密码错误";
            b = false;
        }
        else {
            msg = "账号密码都错误";
            b=false;
        }
        return b;
    
    }

4、ref参数

    static void Main(string[] args)
    {
        // out参数 / ref参数
        //out参数侧重于在方法内部进行赋值,并且返回给调用者。
        //ref是把变量的值从外面带到方法中进行改变,然后再将改变后的值,带出去
    
        //out参数 方便于方法返回不同的参数
        //ref 将传入的方法的参数数据时,传入地址
        double salary = 5000;
    
    
        //double newSalary = Test(salary);
        //JiangJin(ref salary);
        FaKuan(ref salary);
        //Console.WriteLine(newSalary);
    
        Console.WriteLine(salary);
        Console.ReadKey();
    
    }
    public static double Test(double s)
    {
        s += 500;
        return s;
    }
    
    public static void JiangJin(ref double s)//形参
    {
        s += 500;
    }
    
    public static void FaKuan(ref double s)
    {
        s -= 500;
    }

5、Param可变参数数组

    static void Main(string[] args)
    {
        // out参数 / ref参数
        //out参数侧重于在方法内部进行赋值,并且返回给调用者。
        //ref是把变量的值从外面带到方法中进行改变,然后再将改变后的值,带出去
    
        //out参数 方便于方法返回不同的参数
        //ref 将传入的方法的参数数据时,传入地址
        double salary = 5000;
    
    
        //double newSalary = Test(salary);
        //JiangJin(ref salary);
        FaKuan(ref salary);
        //Console.WriteLine(newSalary);
    
        Console.WriteLine(salary);
        Console.ReadKey();
    
    }
    public static double Test(double s)
    {
        s += 500;
        return s;
    }
    
    public static void JiangJin(ref double s)//形参
    {
        s += 500;
    }
    
    public static void FaKuan(ref double s)
    {
        s -= 500;
    }


































  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值