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);
                }
            }
        }
    }
目录
相关文章
|
20天前
|
C#
C#的小例子和字符串(一)
C#的小例子和字符串(一)
29 0
|
1月前
|
C#
C#有关字符串的分割,替换,截取
C#有关字符串的分割,替换,截取
|
4月前
|
存储 安全 编译器
C# 11.0中的泛型属性:类型安全的新篇章
【1月更文挑战第23天】C# 11.0引入了泛型属性的概念,这一新特性为开发者提供了更高级别的类型安全性和灵活性。本文将详细探讨C# 11.0中泛型属性的工作原理、使用场景以及它们对现有编程模式的改进。通过深入了解泛型属性,开发者将能够编写更加健壮、可维护的代码,并充分利用C#语言的最新发展。
|
4月前
|
C# 开发者
C# 10.0引入常量插值字符串:编译时确定性的新篇章
【1月更文挑战第22天】在C# 10.0中,微软为开发者带来了一项引人注目的新特性——常量插值字符串。这一功能允许在编译时处理和计算字符串插值表达式,从而得到可以在编译时确定的常量字符串。本文将深入探讨C# 10.0中常量插值字符串的概念、工作原理、使用场景及其对现有字符串处理方式的改进,旨在帮助读者更好地理解和应用这一强大的新特性。
|
4月前
|
编译器 C# 开发者
C# 10.0中插值字符串的改进:灵活性与性能的双重提升
【1月更文挑战第19天】C# 10.0带来了对插值字符串的显著改进,进一步增强了这一功能的灵活性和性能。插值字符串是C#中处理字符串格式化的一种强大方式,它允许开发者直接在字符串中嵌入变量和表达式。在C# 10.0中,插值字符串不仅获得了语法上的简化,还通过新的编译时优化提高了运行时性能。本文将详细探讨C# 10.0中插值字符串的改进内容,以及这些改进如何为开发者带来更加高效和便捷的编程体验。
|
20天前
|
C#
C#字符串
C#字符串
15 0
|
22天前
|
安全 算法 测试技术
C#编程实战:项目案例分析
【4月更文挑战第20天】本文以电子商务系统为例,探讨C#在实际项目中的应用。通过面向对象编程实现组件抽象和封装,确保代码的可维护性和可扩展性;利用安全性特性保护用户数据;借助数据库操作处理商品信息;通过逻辑控制和算法处理订单;调试工具加速问题解决,展现C#的优势:面向对象、数据库交互、数据安全和开发效率。C#在实际编程中展现出广泛前景。
|
2月前
|
存储 C#
C#中的序列化和反序列化案例
C#中的序列化和反序列化案例
14 0
|
2月前
|
C#
C# 字节数组与INT16,float,double之间相互转换,字符数组与字符串相互转换,
C# 字节数组与INT16,float,double之间相互转换,字符数组与字符串相互转换,
39 1
|
2月前
|
存储 安全 Java
34.C#:listT泛型集合
34.C#:listT泛型集合
18 1