01---Net基础加强

声明两个变量:int n1 = 10, n2 = 20;要求将两个变量交换,最后输出n1为20,n2为10。交换两个变量,使用第三个变量!

    class Program
    {
        static void Main(string[] args)
        {
            int n1 = 10;
            int n2 = 20;
            int temp = n1;
            n1 = n2;
            n2 = temp;
            Console.WriteLine("n1:{0},n2:{1}",n1,n2);
            Console.ReadKey();
        }
    }

声明两个变量:int n1 = 10, n2 = 20;要求将两个变量交换,最后输出n1为20,n2为10。交换两个变量,不使用第三个变量!

 class Program
    {
        static void Main(string[] args)
        {
            int n1 = 10;
            int n2 = 20;
            n1 = n1 + n2;
            n2 = n1 - n2;
            n1 = n1 - n2;
            Console.WriteLine("n1:{0},n2:{1}", n1, n2);
            Console.ReadKey();
        }
    }

声明两个变量:int n1 = 10, n2 = 20;要求将两个变量交换,最后输出n1为20,n2为10。交换两个变量,使用方法!  

    class Program
    {
        static void Main(string[] args)
        {
            int n1 = 10;
            int n2 = 20;
            Swap(ref n1, ref n2);//方法存根快捷键Ctrl+K+M建立方法   ref表示按引用传递
            Console.WriteLine("n1:{0},n2:{1}", n1, n2);
            Console.ReadKey();
        }

        static void Swap(ref int num1, ref int num2)
        {
            int temp = num1;
            num1 = num2;
            num2 = temp;
        }
    }

请用户输入一个字符串,计算字符串中的字符个数,并输出。(“你好ycz” 的字符个数为5)字符串的Length属性表示字符串中字符的个数,无论中文字符还是英文字符,一个字符就是一个字符,不是字节数。

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("请输入一个字符串");
            string msg = Console.ReadLine();
            Console.WriteLine("字符个数为:{0}", msg.Length);
            Console.ReadKey();
        }
    }

定义方法来实现:计算两个数的最大值。提示:方法有几个参数?返回值是什么?      ···········定义方法时一定要有参数和返回值!

class Program
    {
        static void Main(string[] args)
        {
            int max = GetMax(100,101);
            Console.WriteLine("最大值为:{0}", max);
            Console.ReadKey();
        }

        static int GetMax(int n1,int n2)
        {
            return n1>n2?n1:n2;
        }
    }

计算任意多个数间的最大值(提示:params)。

    class Program
    {
        static void Main(string[] args)
        {
            int max = GetMax(100,101,22,24,35,333);
            Console.WriteLine("最大值为:{0}", max);
            Console.ReadKey();
        }

        static int GetMax(params int[] nums)//params可变参数
        {
            int max=nums[0];
            for (int i = 1; i < nums.Length; i++)
            {
             if(nums[i]>max)
                max=nums[i];
            }
            return max;
        }
    }

用方法来实现:计算1-100之间的所有整数的和。

 class Program
    {
        static void Main(string[] args)
        {
           int sum= GetSum(1,100);
            Console.WriteLine(sum);
            Console.ReadKey();
        }

        static int GetSum(int StartNumber,int EndNumber)
        {
            int sum = 0;
            for (int i = StartNumber; i <= EndNumber; i++)
            {
                sum += i;
            }
            return sum;
        }
    }

用方法来实现:计算1-100之间的所有奇数的和。

        static void Main(string[] args)
        {
           int sum= GetOddSum(1,100);
            Console.WriteLine(sum);
            Console.ReadKey();
        }

        static int GetOddSum(int StartNumber,int EndNumber)
        {
            int sum = 0;
            for (int i = StartNumber; i <= EndNumber; i++)
            {
                if (i % 2 != 0)
                {
                    sum += i;
                }
            }
            return sum;
        }

用方法来实现:判断一个给定的整数是否为“质数”;

 class Program
    {
        static void Main(string[] args)
        {
            string s;
            Console.WriteLine("请输入一个正整数:");
            int n = Convert.ToInt32(Console.ReadLine());
            if (IsZhiShu(n))
            {
                s = "这个数是质数";
                Console.WriteLine(s);
                Console.ReadKey();
            }
            else
            {
                s = "这个数不是质数";
                Console.WriteLine(s);
                Console.ReadKey();
            }
        }

        static bool IsZhiShu(int num)
        {
            bool b = true;
            for (int i = 2; i <= Math.Sqrt(num); i++)  //对num开根号
            {
                if (num % i == 0)
                {
                   b=false;
                }
            }
            return b;
        }

用方法实现:计算1-100之间所有质数的和。

   class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            for (int i = 2; i <= 100; i++)
            {
                // 对于每个数字判断是否是一个质数
                if (IsZhiShu(i))
                {
                    sum += i;
                }
            }
            Console.WriteLine(sum);
            Console.ReadLine();
        }

        static bool IsZhiShu(int num)
        {
            bool b = true;
            for (int i = 2; i <=Math.Sqrt(num); i++)  //对num开根号
            {
                if (num % i == 0)
                {
                   b=false;
                }
            }
            return b;
        }
    }

用方法来实现:有一个数组,找出其中最大值并输出。不能调用数组的Max()方法。

    class Program
    {
        static void Main(string[] args)
        {
            int[] arrInt = {1,3,5,66,6,8,99,55 };
            int max = GetMax(arrInt);
            Console.WriteLine(max);
            Console.ReadLine();
        }

        static int GetMax(int[] arr)
        {
            int max = arr[0];
            for (int i = 1; i < arr.Length;i++ )
            {
                if (arr[i] > max)
                {
                    max = arr[i];
                }
            }
            return max;
        }
    }
用方法来实现:有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出字符数最多的字符串。
 class Program
    {
        static void Main(string[] args)
        {
            string[] arrName = { "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" };
            string  maxName = GetMaxString(arrName);
            Console.WriteLine(maxName);
            Console.ReadLine();
        }

        static string GetMaxString(string[] arrName)
        {
            string maxName = arrName[0];
            for (int i = 1; i < arrName.Length;i++ )
            {
                if (arrName[i].Length > maxName.Length)
                {
                    maxName = arrName[i];
                }
            }
            return maxName;
        }
    }

用方法实现:请计算出一个整型数组的平均值。{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }。要求:计算结果如果有小数,则显示小数点后两位(四舍五入)。Math.Round()

    class Program
    {
        static void Main(string[] args)
        {
            int[] arrInt = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10};
            Console.WriteLine(GetAvg(arrInt));
            Console.ReadLine();
        }

        static double GetAvg(int[] arr)
        {
            int sum = 0;
            for (int i = 1; i < arr.Length;i++ )
            {
               sum += arr[i];
            }
            return Math.Round((double)sum/ arr.Length, 2); //Math.Round()对数字执行四舍五入,保存两位小数。注意*两个整数相除结果还是整数,可以将一个强制转换成double类型
        }
    }

通过冒泡排序法对整数数组{ 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 }实现升序排序。

  class Program
    {
        static void Main(string[] args)
        {
            int[] arrInt = { 1, 3, 5, 7, 90, 2, 4, 6, 8, 10 };
            for (int i = 0; i < arrInt.Length-1; i++)
            {
                for (int j = arrInt.Length - 1; j > i; j--)
                {
                    if (arrInt[j] < arrInt[j - 1])
                    {
                        int temp = arrInt[j];
                        arrInt[j] = arrInt[j - 1];
                        arrInt[j - 1] = temp;
                    }
                }
            }

            for (int i = 0; i < arrInt.Length; i++)
            {
                Console.WriteLine(arrInt[i]);
            }
            Console.ReadLine();
        }
    }

有如下字符串:【"患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?”"】。需求:①请统计出该字符中“咳嗽”一词的出现次数,以及每次“咳嗽”出现的索引位置。②扩展(*):统计出每个字符的出现次数。    JavaScript indexOf() 方法

    class Program
    {
        static void Main(string[] args)
        {
            string msg = "患者:“大夫,我咳嗽得很重。” 大夫:“你多大年记?” 患者:“七十五岁。” 大夫:“二十岁咳嗽吗”患者:“不咳嗽。” 大夫:“四十岁时咳嗽吗?” 患者:“也不咳嗽。” 大夫:“那现在不咳嗽,还要等到什么时咳嗽?";
            string keywords="咳嗽";
            int count = 0;
            int index = 0;
            while ((index = msg.IndexOf(keywords, index)) != -1)//使用IndexOf(),该方法返回在整个字符串中,指定的字符或字符串第一次出现的索引位置,如果没有找到指定的字符或者字符串则返回-1,如果找到了返回索引位置
            {
                count++;
                Console.WriteLine("第{0}次,出现【咳嗽】,索引时{1}", count,index);
                index = index + keywords.Length;
            }
            Console.WriteLine("【咳嗽】一词,共出现了{0}次,", count);
            Console.ReadKey();
        }
    }

将字符串"      hello world,你 好 世界 !      "两端空格去掉,并且将其中的所有其他空格都替换成一个空格,输出结果为:"hello world,你 好 世界 !"。 Trim用法  Split用法Join用法

    class Program
    {
        static void Main(string[] args)
        {
            string msg = "       Hello World       你  好    世界     ";
            msg = msg.Trim();
            string[] words = msg.Split(new char[]{‘ ‘},StringSplitOptions.RemoveEmptyEntries);
            string full = string.Join(" ", words);
            Console.WriteLine(full);
            Console.ReadKey();
        }
    }

制作一个控制台小程序。要求:用户可以在控制台录入每个学生的姓名,当用户输入quit(不区分大小写)时,程序停止接受用户的输入,并且显示出用户输入的学生的个数,以及每个学生的姓名。并统计姓王的个数!

class Program
    {
        static void Main(string[] args)
        {
            List <string> ListNames=new List<string>();//初始化一个集合
            int count = 0;
            while (true)
            {
                Console.WriteLine("请输入姓名:");
                string name = Console.ReadLine();
                if (name[0] == ‘王‘)
                {
                    count++;
                }
                if (name.ToLower() == "quit")
                {
                    break;
                }
                ListNames.Add(name);
            }
            Console.WriteLine("你共输入了:{0}个名字",ListNames.Count);
            foreach (string name in ListNames)
            {
                Console.WriteLine(name);
            }
            Console.WriteLine("姓王的同学的个数:{0}", count);
            Console.ReadKey();
        }
    }

将普通日期格式:“2014年7月8日”转换成汉字日期格式:“二零一四年七月八日”。(暂时不考虑10日,13日,23日)

 class Program
    {
        static void Main(string[] args)
        {
            string date = "2014年7月8日";
            date = ConvertDate(date);
            Console.WriteLine(date);
            Console.ReadKey();
        }

        private static string ConvertDate(string date)  //字符串具有不可变性,不能直接修改字符串
        {
            //将字符串转换成一个真正的char数组
            char[] chs = date.ToCharArray();
            for (int i = 0; i < date.Length;i++ )
            {
                switch (chs[i])
                {
                    case ‘0‘:
                             chs[i] = ‘零‘;
                             break;
                    case ‘1‘:
                             chs[i] = ‘一‘;
                             break;
                    case ‘2‘:
                             chs[i] = ‘二‘;
                             break;
                    case ‘3‘:
                             chs[i] = ‘三‘;
                             break;
                    case ‘4‘:
                             chs[i] = ‘四‘;
                             break;
                    case ‘5‘:
                             chs[i] = ‘五‘;
                             break;
                    case ‘6‘:
                             chs[i] = ‘六‘;
                             break;
                    case ‘7‘:
                             chs[i] = ‘七‘;
                             break;
                    case ‘8‘:
                             chs[i] = ‘八‘;
                             break;
                    case ‘9‘:
                             chs[i] = ‘九‘;
                             break;
                }
            }
            return new string(chs);
        }
    }

创建一个Person类,属性:姓名、性别、年龄;方法:SayHi() 。再创建一个Employee类继承Person类,扩展属性Salary,重写SayHi方法。

 class Person
    {
        private string _name;
        public string Name
        {
            get
            {
                return _name;
            }
            set
            {
                _name = value;
            }
        }

        public bool Gender { get; set;}
        public int Age{ get; set;}

        public virtual void SayHi()  //加上virtual,子类才能重写它
        {
            Console.WriteLine("Hi!!!!!!");
        }
    }
 class Employee:Person
    {
        public double Salary { get; set; }

        public override void SayHi()//对这个方法重写
        {
            Console.WriteLine("子类中的SayHi!!!");
        }
    }

请编写一个类:ItcastClasss,该类中有一个私有字段_names.数据类型为字符串数组,数组长度为5,并有5个默认的姓名。要求:为ItcastClass编写一个索引器,要求该索引器能够通过下标访问_names中的内容。

  class ItCastClass
    {
        private string[] _names = { "叶长重", "王少伟", "杨中科", "苏坤", "科比" };

        public string this[int index]  //索引器
        {
            set  {
                _names[index] = value;
                 }
            get {
                return _names[index];
                }
        }
    }
static void Main(string[] args)
        {
            ItCastClass itcast = new ItCastClass();
            //Console.WriteLine(itcast._names[0]);//没有索引器的情况下得这样写!
            Console.WriteLine(itcast[0]);
            Console.WriteLine(itcast[3]);
            Console.ReadKey();
        }

使用WinForm窗体,制作一个简单的计算器,默认值为“请选择”。要求具有+、-、*、/功能!

  public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void Form1_Load(object sender, EventArgs e)
        {
            this.comboBox1.SelectedIndex = 0;
        }

        private void button1_Click(object sender, EventArgs e)
        {
            //校验用户选择的“操作符”
            if (comboBox1.SelectedIndex > 0)
            {
                int n1 =Convert.ToInt32( textBox1.Text.Trim());
                int n2 = Convert.ToInt32(textBox2.Text.Trim());
                //int n2 = int.Parse(textBox2.Text.Trim());//这个也可以
                switch (comboBox1.Text)
                {
                    case "+":
                        label1.Text = (n1 + n2).ToString();
                        break;
                    case "-":
                        label1.Text = (n1 - n2).ToString();
                        break;
                    case "*":
                        label1.Text = (n1 * n2).ToString();
                        break;
                    case "/":
                        label1.Text = (n1 / n2).ToString();
                        break;
                    default:
                        break;
                }
            }
            else
            {
                MessageBox.Show("请选择操作符");
            }
        }
    }

什么是方法重载。(方法重载1.方法名必须一致。2,方法参数列表不同)

请将字符串数组(“中国”,“美国”,“巴西”,“澳大利亚”,“加拿大”)中的内容反转,然后输出反转后的数组,不能用数组的Reverse()方法。

  static void Main(string[] args)
        {
            string[] names = { "中国", "美国", "巴西", "澳大利亚", "加拿大" };
            ReverseArray(names);
            for (int i = 0; i < names.Length; i++)  //快捷键 打上for再按Tab键
            {
                Console.WriteLine(names[i]);
            }
            Console.ReadKey();
        }

        private static void ReverseArray(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;
            }
        }
    }

编程遍历WinForm窗体上所有TextBox控件并给它赋值为“叶长重”。

 public partial class Form1 : Form
    {
        public Form1()
        {
            InitializeComponent();
        }

        private void button1_Click(object sender, EventArgs e)
        {
            foreach (Control ctrl in this.Controls)//遍历这个窗体中所有控件
            {
                //判断控件是不是文本框
                if (ctrl is TextBox)
                {
                    ctrl.Text = "叶长重";
                }
            }
        }
    }

随机生成10个1-100内的非重复数字放入ArrayList集合中。

  class Program
    {
        static void Main(string[] args)
        {
            int count = 0;
            ArrayList arrList = new ArrayList();
            Random rdm = new Random();//()内不能放参数,不然会产生一样的,里面是种子,种子确定,产生的数是一样的!
            while (arrList.Count < 10)
            {
                //Random rdm = new Random();//这句话不能放到这里,系统用当前时间做种子
                count++;
               int n = rdm.Next(1, 101);
               if (!arrList.Contains(n))
               {
                   arrList.Add(n);
               }
            }
            for (int i = 0; i < arrList.Count; i++)
            {
                Console.WriteLine(arrList[i]);//快捷键 cw+Table
            }
            Console.WriteLine("循环了{0}次",count);
            Console.ReadKey();
        }
    }

.net程序基本编写、执行流程(c#)

1>编写c#代码,保存为.cs文件。
          2>通过csc.exe程序来将.cs文件编译为.net程序集(.exe或.dll)。此时的exe或dll并不是机器码(cpu不可理解)。【>csc /out:c:\a.exe c:\program.cs】
          3>程序运行时通过JIT编译(Just In Time)即时编译,将程序集编译为cpu能理解的机器码,这时cpu才能执行。(这个编译过程会与当前机器有关(根据当前机器的内存、cpu等))。ngen.exe

面向对象(OO)

什么是面向对象?一种分析问题的方式(增强了程序的可扩展性)。
          面向对象三大特性:封装、继承、多态。
          什么是类?什么是对象?类和对象的区别?如何写一个汽车类?
          类是模具,创建对象的模具,抽象的。
          类是一种数据类型,用户自定义的数据类型
          类组成:字段、属性、方法、构造函数等
          对象是具体的,是类的具体实例。对象具有属性(特征)和方法(行为)
          类中包含了数据(用字段表示)与行为(用方法(函数、功能)表示,方法为一块具有名称的代码)  (先用类后有对象)
添加一个教师类和一个学生类:

  public  class Teacher
    {
        //构造函数特点:1、函数名和类名完全一样 2、不能有返回值,哪怕是void 3、一般访问修饰符为public
        public Teacher()
        { 

        }
        //构造函数重载
        public Teacher(string name)
        {

        }
        public string Name { get; set; }
        public int  Age { get; set; }
        public void Teach()
        {
            Console.WriteLine("上课。。。");
        }
    }
    class Student
    {
        public string Name { get; set; }
        public string  SId { get; set; }
        public void shangke()
        {
            Console.WriteLine("上课。。。");
        }
    }
  class Program
    {
        static void Main(string[] args)
        {
            //当写好一个类以后就会有一个默认的无参的构造函数。
            Teacher t = new Teacher();
        }
    }
时间: 2024-08-13 08:53:30

01---Net基础加强的相关文章

01背包基础 (杭电2602)

01背包问题: 有一个体积为V的背包,有n件物品,每件物品的体积,价值分别为w[i],p[i];要从n件物品中选些放入背包中,使背包里物品的总价值最大. 动态方程:c[i][j]=max(c[i-1][j],c[i-1][j-w[i]]+p[i]). 有关动态方程方面的代码: for (int i = 1; i <= n; i++) { for (int j = 1; j <= total_weight; j++) { if (w[i] > j) { c[i][j] = c[i-1][j

01 动态链接库基础

DLL是一个包含函数和数据的模块, 它们可以被其他模块(应用程序或DLL)使用. DLL可以定义两种函数: <1>导出函数   <2>内部函数 . 导出函数可以被内部或其他模块调用. 内部函数只能在DLL内部被调用. About Dynamic-Link Libraries 动态连接 允许一个模块在加载或运行时,仅仅只需包含定位一个动态库导出函数的信息,而无需将这个动态库整个编译进模块中. 调用一个DLL中的函数,有两种方法: <1> load-time dynamic

Jam&#39;s balance HDU - 5616 (01背包基础题)

Jim has a balance and N weights. (1≤N≤20) The balance can only tell whether things on different side are the same weight. Weights can be put on left side or right side arbitrarily. Please tell whether the balance can measure an object of weight M. In

后端 - Lession 01 PHP 基础

目录 Lession 01 php 基础 1. php 基础 2. php 变量 3. php 单引号 和 双引号区别 4. 数据类型 5. 数据类型转换 6. 常量 7. 运算符 8. 为 false 的几种情况(条件判断) 9. 流程判断 10. 循环 11.1 函数 11.2 函数的 行参 和 返回值 12. 常用函数 Lession 01 php 基础 1. php 基础 phpinfo():输出版本号 echo:输出文本 php.ini:php的配置文件 2. php 变量 命名变量必

shell编程01—shell基础

01.学习shell编程需要的知识储备 1.vi.vim编辑器的命令,vimrc设置 2.命令基础,100多个命令 3.基础.高端的网络服务,nfs,rsync,inotify,lanmp,sersync,sshkey批量分发管理 02.shell脚本概念 1.什么是shell shell是一个命令解释器,在操作系统的最外层,负责直接与用户对话,将用户的输入解释给操作系统,并输出操作系统各种各样的处理结果,输出到屏幕返回给用户.这种对话方式可与是交互式的(键盘输入命令,可以立即得到shell的回

&lt;&lt;Python基础教程&gt;&gt;学习笔记之|第01章|基础知识

本学习笔记主要用要记录下学习<<Python基础教程>>过程中的一些Key Point,或自己没怎么搞明白的内容,可能有点杂乱,但比较实用,查找起来也方便. 第01章:基础知识 ------ Jython:      Python的Java实现,运行在JVM中,相对稳定,但落后于Python,当前版本2.5,在TA(Python+Robot)会用到 IronPython:  Python的C#实现,运行在Common Language Runtime,速度比Python要快 >

01 mysql基础一 (进阶)

mysql基础一 1.认识mysql与创建用户 01 Mysql简介 Mysql是最流行的关系型数据库管理系统之一,由瑞典MySQLAB公司开发,目前属于Oracle公司. MySQL是一种关联数据库管理系统,关联数据库将数据保存在不同的表中,而不是将所有数据放在一个大仓库内,这样就增加了速度并提高了灵活性. (开源,免费) #关系型数据库:采用关系模型来组织数据的数据库 #关系:一张二维表,每个关系都有一个关系名,就是表名,互相关联 #模型:行和列(二维),具体指字段跟字段信息 02 进入my

Python入门学习 DAY 01 计算机基础

Python入门 DAY 01 作为一名刚刚学习python的小白,我首先去学习的并不是python语言的基础知识,而是先对计算机的基础进行了一个初步的了解,以下内容便是在学习python之前我去学习到的大致内容. 1.什么是编程语言    语言是一个事物与另外一个事物沟通的介质    编程语言是程序员与计算机沟通的介质    2.什么是编程    编程就是程序按照某种编程语言的语法规范将自己想要让计算机做的事情表达出来    表达的结果就是程序,程序就是一系列的文件    3.为什么要编程  

01前端基础入门

01 基本网格界面显示 1 <!-- 2 作者:offline 3 时间:2018-09-04 4 描述:html基本表格界面设计 5 在最基本的界面设计中,先套用表格界面,再在表格的基础上向里面添加图片和文字 6 因此在设计之初就需要设计好整个模块的表格嵌套结构 7 行之间的合并:colspan 8 列之间的合并:rowplan 9 被合并的单元格必须要从代码中删除 10 因此在布局中有一般是先细分表格多做单元格,然后进行单元格的合并 11 --> 12 13 <!DOCTYPE ht

01.Java基础问题

目录介绍 1.0.0.1 请手写equal方法,讲讲具体的原理?1.0.0.2 请说下String与StringBuffer区别,StringBuffer底部如何实现?String类可以被继承吗,为什么?1.0.0.3 String a=""和String a=new String("")的的关系和异同?String的创建机制?1.0.0.4 static关键字可以修饰什么?static使用的注意事项有哪些?static关键字的特点?1.0.0.5 为什么 Java