C#经典案例实现(求单词在句子出现的次数,字符串倒序输出,用泛型实现两数交换等)

简介: (求单词在句子出现的次数,字符串倒序输出,用泛型实现两数交换等)

求单词在句子出现的次数

class Cishu
    {
        public static void Getnum()
        {
            string str="The quick brown fox jumped over the lazy dog keeps the doctor away.Can a fox and a dog be friends?";
            string key="the";
            int count=0;
            for(int i = 0; i < str.Length - key.Length; i++)
            {
                if (str.Substring(i, key.Length) == key)
                {
                    count++;
                }
            }
            Console.WriteLine(key +"在" +str+"中出现" + count+"次");

        }
    }

字符串倒序输出

class Daoxu
    {
        public static void Input()
        {
            string str = "this is string";
            char[] ch= str.ToArray();
            Array.Reverse(ch);
            str = new string(ch);
            Console.WriteLine(str);
        }
    }

用泛型实现两数交换

class Program
    {
        static void Swap<T>(ref T x,ref T y)
            {
                T Z = x;
                 x = y;
                 y = Z;

            }
        public static void Main(string[] args)
        {


            

            string x = "hello";
            string y = "world";
            Console.WriteLine("{0},{1}", x, y);
            Swap<string>(ref x, ref y);
            Console.WriteLine("{0},{1}", x, y);
            Console.ReadKey();


           

           




        }
       

    }

求雇员们的总工资

class Employee
    {
      public string empName { get; set; }
       public float empWage { get; set; }
        public static void Main(string[] args)
        {
            List<Employee> list = new List<Employee>();
            list.Add(new Employee() { empName = "李一", empWage = 20 });
            list.Add(new Employee() { empName = "张三", empWage = 30 });
            list.Add(new Employee() { empName ="王二",  empWage= 50 });
            list.Add(new Employee() { empName ="赵四",   empWage=90});
            float sum=0;
            list.ForEach(x => { sum += x.empWage; });
            Console.WriteLine(sum);

        }


    }

还原

class Huanyuan
    {
        public static void Huan()
        { 
            for(int i = 100; i <=999; i++)
            {
                int j = i * 100 + 95;
                if(j%57==0 && j % 67 == 0)
                {
                    Console.WriteLine(j);
                }
            }
        }
    }

求圆的周长,面积,体积

 class MyMath
    {
       
        public static void Perimeter(double R)
        {
            double c = 2 * Math.PI * R;
            Console.WriteLine(c);
        }
        public static void Area(double R)
        {
            double s = Math.PI * R * R;
            Console.WriteLine(s);
        }
        public static void Voiume(double R)
        {
            double v= (3/4)*Math.PI*R*R*R;
            Console.WriteLine(v);
        }

百钱百鸡

class Maiji
    {
        public static void Buy()
        {
            for(int i = 0; i < 20; i++)
            {
                for (int j = 0; j < 33; j++)
                {
                    if ((100 - i - j)%3==0 && i * 5+ j * 3+ (100 - i - j) / 3 == 100)
                    {
                        Console.WriteLine("{0},{1},{2}", i, j, 100 - i - j);
                    }
                }
            }
        }
    }

输出年份

class Nianfen
    {
        public static void Put()
        {
            float i = 1.334f;
            int n = 123;
            string s1 = string.Format("{0:F3}", i);
            Console.WriteLine(s1);
            string s2 = string.Format("{0:p}", i);
            Console.WriteLine(s2);
            string s3 = string.Format("{0:00000}", n);
            Console.WriteLine(s3);
            DateTime dt = new DateTime(2020, 9, 24);
            string str = dt.ToString("yyyy年MM月d日");
            Console.WriteLine(str);
        }
    
    }

求100以内的质数

class Zishu
    {
        public static void Shu()
        {
            for(int i = 2; i <= 100; i++)
            {
                int j = 2;
                while (i % j != 0)
                {
                    j++;
                }
                if (i == j)
                {
                    Console.WriteLine(i);
                }
            }
        }
    }
目录
相关文章
|
5月前
|
C#
C#的小例子和字符串(一)
C#的小例子和字符串(一)
139 0
|
5月前
|
存储 安全 编译器
C# 11.0中的泛型属性:类型安全的新篇章
【1月更文挑战第23天】C# 11.0引入了泛型属性的概念,这一新特性为开发者提供了更高级别的类型安全性和灵活性。本文将详细探讨C# 11.0中泛型属性的工作原理、使用场景以及它们对现有编程模式的改进。通过深入了解泛型属性,开发者将能够编写更加健壮、可维护的代码,并充分利用C#语言的最新发展。
|
5月前
|
C#
C#有关字符串的分割,替换,截取
C#有关字符串的分割,替换,截取
|
6天前
|
开发框架 .NET 程序员
C# 去掉字符串最后一个字符的 4 种方法
在实际业务中,我们经常会遇到在循环中拼接字符串的场景,循环结束之后拼接得到的字符串的最后一个字符往往需要去掉,看看 C# 提供了哪4种方法可以高效去掉字符串的最后一个字符
|
2月前
|
数据采集 存储 C#
C# 爬虫技术:京东视频内容抓取的实战案例分析
C# 爬虫技术:京东视频内容抓取的实战案例分析
|
23天前
|
安全 程序员 编译器
C#一分钟浅谈:泛型编程基础
在现代软件开发中,泛型编程是一项关键技能,它使开发者能够编写类型安全且可重用的代码。C# 自 2.0 版本起支持泛型编程,本文将从基础概念入手,逐步深入探讨 C# 中的泛型,并通过具体实例帮助理解常见问题及其解决方法。泛型通过类型参数替代具体类型,提高了代码复用性和类型安全性,减少了运行时性能开销。文章详细介绍了如何定义泛型类和方法,并讨论了常见的易错点及解决方案,帮助读者更好地掌握这一技术。
43 11
|
16天前
|
前端开发 C#
C# 一分钟浅谈:字符串操作与正则表达式
本文详细介绍C#中的字符串操作与正则表达式应用,涵盖字符串拼接、分割、查找及替换等基础操作,并通过实例讲解正则表达式的模式匹配、文本替换与分组捕获技巧。同时,文章还探讨了性能优化、复杂度管理和安全性等问题及解决策略,助你提升编程效率,应对实际开发挑战。
50 0
|
2月前
|
编译器 C#
C#中内置的泛型委托Func与Action
C#中内置的泛型委托Func与Action
50 4
|
3月前
|
SQL 开发框架 前端开发
在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式
在C#开发中使用第三方组件LambdaParser、DynamicExpresso、Z.Expressions,实现动态解析/求值字符串表达式
|
2月前
|
C#
C# 面向对象编程(三)——接口/枚举类型/泛型
C# 面向对象编程(三)——接口/枚举类型/泛型
26 0