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();
        }
    }

利用执行过程--压栈

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

声明两个变量,交换两个变量,使用方法! 

  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();
        }

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

计算1+2-3+4-5+6-7+.........100值。

 class Program
    {
        static void Main(string[] args)
        {
            int sum = 0;
            for (int i = 1; i <=100; i++)
            {
                if (i == 1 || i % 2 == 0)
                {
                    sum += i;
                }
                else
                {
                    sum -= i;
                }
            }
            Console.WriteLine(sum);
            Console.ReadKey();
        }
    }

请用户输入一个字符串,计算字符串中的字符个数,并输出。(“你好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之间的所有奇数的和。

 class Program
    {
        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;
        }
    }

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

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

用方法来实现:有一个字符串数组:{ "马龙", "迈克尔乔丹", "雷吉米勒", "蒂姆邓肯", "科比布莱恩特" },请输出字符数最多的字符串。

    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();
        }
    }

自己编写函数实现十进制转成二进制

 class Program
    {
        static void Main(string[] args)
        {

            while (true)
            {
                Console.WriteLine("请输入一个十进制数字:");
                int num = Convert.ToInt32(Console.ReadLine());
                string result = GetBinaryNumber(num);
                Console.WriteLine(result);
            }
        }

        private static string GetBinaryNumber(int num)
        {
            ////拼接字符串
            //StringBuilder sb = new StringBuilder();
            //while (num >= 2)
            //{
            //    ////    int shang = num / 2;
            //    //余数
            //    int yushu = num % 2;
            //    sb.Append(yushu);
            //    //把本次计算的商赋值给num变量
            //    num = shang;
            //}
            //sb.Append(num);           
            //return sb.ToString();

            List<string> list = new List<string>();
            while (num >= 2)
            {
                //
                int shang = num / 2;
                //余数
                int yushu = num % 2;
                list.Add(yushu.ToString());
                //把本次计算的商赋值给num变量
                num = shang;
            }
            list.Add(num.ToString());
            list.Reverse();
            return string.Join("", list.ToArray());
        }
    }

十进制转成二进制(系统方法)

    class Program
    {
        static void Main(string[] args)
        {
            DateTime begin = DateTime.Now;
            int i = 100;
            string j = Convert.ToString(i, 2);//j就是转换后的二进制了!!
            Console.WriteLine(j);
            DateTime end = DateTime.Now;
            Console.WriteLine("计算耗时:" + (end - begin) + "");
            Console.ReadLine();
        }
    }

判断瑞年

 class Program
    {
        static void Main(string[] args)
        {

            while (true)
            {
                Console.WriteLine("请输入一个年份:");
                int year = Convert.ToInt32(Console.ReadLine());
                bool b = IsLeapYear(year);
                Console.WriteLine(b);
            }
        }

        private static bool IsLeapYear(int year)
        {
            if (year%4==0 && year%100!=0 || year%400==0)
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }

乘法口诀表

    class Program
    {
        static void Main(string[] args)
        {
            for (int i = 1; i <= 9; i++)
            {
                for (int j = 1; j <= i; j++)
                {
                    Console.Write("{0}*{1}={2} 	", j, i, j * i); //  	 水平制表位(horizeontal tab),将光标移到最接近8的倍数的位置,使得后面的输入从此开始。换句话说,让所有的数据都紧跟在制表符后面输出。
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
    }

自己编写一个函数,实现类似.net中Trim()函数的功能;去掉字符串两端的空格

    class Program
    {
        static void Main(string[] args)
        {
            //string msg = "   你 好 吗?   ";
            //Console.WriteLine("========" + msg.Trim() + "===========");
            //Console.ReadKey(); 

            string msg = "   你 好 吗?   ";
            Console.WriteLine("========" + MyTrim(msg) + "===========");
            Console.ReadKey();
        }

        private static string MyTrim(string msg)
        {
            int start = 0;
            int end = msg.Length - 1;
            //寻找第一个不是空白符的字符的索引,并保存到start变量中
            while (start < msg.Length)
            {
                if (!char.IsWhiteSpace(msg[start]))
                {
                    break;
                }
                start++;
            }

            //寻找最后一个不是空白符的字符的索引,并保存到end变量中

            while (end >= start)
            {
                if (!char.IsWhiteSpace(msg[end]))
                {
                    break;
                }
                end--;
            }
            return msg.Substring(start, end - start + 1);
        }
    }

随机生成10个1-100内的非重复的偶数放入ArrayList集合中。ArrayList集合:using System.Collections;

 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 (n % 2 == 0 && !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();
        }
    }

  

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

统计数组中不重复元素的个数 

 class Program
    {
        static void Main(string[] args)
        {
            int[] arrInt = {1,2,3,4,5,6,7,5,6,5,45,8,9,85,45,55,2,2,};

            //统计不重复个数的元素
            int count = 0;
            for (int i = 0; i < arrInt.Length; i++)
            {
                bool b = true;
                for (int j = 0; j <=i; j++)
                {
                   if(arrInt[i]==arrInt[j] && i!=j) 
                   {
                       b = false;
                       break;
                   }
                }
                if (b)
                {
                    count++;
                }
            }
            Console.WriteLine("不重复的元素的个数:{0}",count);
            Console.ReadKey();
        }
    }

去掉重复元素方法一

    class Program
    {
        static void Main(string[] args)
        {
            int[] arrInt = { 1, 2, 3, 4, 5, 6, 7, 5, 6, 5, 45, 8, 9, 85, 45, 55, 2, 2, 2, 2, 2, 2, 2, 2, 88,88,88,55};
            //对数组进行排序
            Array.Sort(arrInt);

            List<int> list = new List<int>();
            for (int i = 0; i < arrInt.Length - 1; i++)
            {
                if (arrInt[i] != arrInt[i + 1])
                {
                    list.Add(arrInt[i]);
                }
            }
            list.Add(arrInt[arrInt.Length - 1]);
            for (int i = 0; i < list.Count; i++)
            {
                Console.WriteLine(list[i]);
            }
            Console.ReadKey();
        }
    }

去掉重复元素方法二

    class Program
    {
        static void Main(string[] args)
        {
            int[] arrInt = { 1, 2, 3, 4, 5, 6, 7, 5, 6, 5, 45, 8, 9, 85, 45, 55, 2, 2, 2, 2, 2, 2, 2, 2, 88, 88, 88, 55 };
            //对数组进行排序
            Array.Sort(arrInt);
            arrInt=arrInt.GroupBy(p => p).Select(p => p.Key).ToArray();
            foreach (var item in arrInt)
            {
                Console.WriteLine(item);
            }
            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();
        }
    }
原文地址:https://www.cnblogs.com/yechangzhong-826217795/p/4047679.html