C#



字符串的操作:

结果如图示:

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _3._4字符串操作
{
    class Program
    {
        static void Main(string[] args)
        {
            string str = "This Is An Apple";
            if (!string.IsNullOrEmpty(str))
            {
                char findchar = Convert.ToChar(str.Substring(8, 1));///取出字符串中的第9个字符
                int count = Count(str, findchar);
                string[] word = str.Split(' ');///将字符串按照空格分割成数组求单词数
                int len = word.Length;
                string sReverse = Reverse(str).ToUpper();///反序并大写
                Console.WriteLine("\"This Is An Apple.\"\n共有{0}个单词,\n{1}出现了{2}次", len, findchar, count);
                Console.WriteLine(sReverse);

            }
            else
                Console.WriteLine("字符串为空");
            Console.ReadKey();
        }
        ///统计要找单词的个数
        public static int Count(string s, char findchar)
        {
            int count = 0;
            char[] c = s.ToCharArray();
            for (int i = 0; i < s.Length; i++)
            {
                if (c[i] == findchar)
                    count++;
            }
            return count;

        }
        ///反序输出
        public static string Reverse(string s)
        {
            string str = "";
            char[] c = s.ToCharArray();
            for (int i = c.Length - 1; i >= 0; i--)
            {
                str += c[i].ToString();
            }
            return str;
        }
    }
}



 




random()类猜数游戏:

结果如图示:

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace random类猜数
{
    class Program
    {
        static void Main(string[] args)
        {
            Random rd = new Random();
            int x = rd.Next(0, 10);
            Console.WriteLine("请输入一个0-10的数:");
            for (int i = 0; i < 5; i++)
            {
                int a = int.Parse(Console.ReadLine());
                if (x == a)
                {
                    Console.WriteLine("恭喜您答对了");
                }
                else if (a < x)
                {
                    Console.WriteLine("小了");
                }
                else
                    Console.WriteLine("大了");
            }
            Console.ReadKey();
        }
    }
}









4.2有参无参构造函数的使用

要求结构如图示:

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace _42构造函数的使用
{
    class Program
    {
        static void Main(string[] args)
        {
            Person p1 = new Person();
            Console.WriteLine("调用无参构造函数:");
            p1.ShowMsg();
            Person p2 = new Person("杰克", 20);
            Console.WriteLine("调用有参构造函数:");
            p2.ShowMsg();
            Console.ReadKey();
        }
    }
    class Person
    {
        public string name;
        int age;
        protected string sex;
        public Person(){}
        public Person(string nam, int ag)
        {
            name = nam;
            age = ag;
        }
        public void ShowMsg()
        {
            Console.WriteLine("姓名:{0}\n年龄:{1}", name, age);
        }
    }
}





输出参数(out):

输入一个由若干字符组成的字符串,写一个静态方法,方法中使用输出参数out输出其中的大写,小写,数字和其他字符的个数

结果如图:

代码:


using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 输出参数out统计各种字符个数
{
    class MyClass
    {
        static void Main(string[] args)
        {
            int da, xiao, num, others;
            da = xiao = num = others = 0;
            Console.WriteLine("请输入一个字符串:");
            string s = Console.ReadLine();
            Statistics.Count(s, out da, out xiao, out num, out others);
            Console.WriteLine("大写字母个数:{0}\n小写字母个数:{1}\n数字个数:{2}\n其他字符个数:{3}", da, xiao, num, others);
            Console.ReadKey();
        }
    }
    class Statistics
    {
        public static void Count(string s,out int da, out int xiao, out int num, out int others)
        {
            da = xiao = num = others = 0;
            for (int i = 0; i < s.Length; i++)
            {
                if (s[i] >= 'A' && s[i] <= 'Z')
                    da++;
                else if (s[i] >= 'a' && s[i] <= 'z')
                    xiao++;
                else if (s[i] >= '0' && s[i] <= '9')
                    num++;
                else
                    others++;
            }
        }
    }
}






数组型参数params

输出数组中最大值最小值以及求和

结果如图:

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 数组型参数params
{
    class Program
    {
        static void Main(string[] args)
        {
            int max, min, sum;
            MyClass.GetMaxMinSum(out max,out min ,out sum,1,2,3,4,5,6);
            Console.WriteLine("max={0}\nmin={1}\nsum={2}",max,min,sum);
            int []a={1,2,3,4,5,6,7,8};
            MyClass.GetMaxMinSum(out max,out min ,out sum,a);
            Console.WriteLine("max={0}\nmin={1}\nsum={2}",max,min,sum);
            Console.ReadKey();
        }
    }
    class MyClass
    {
        public static void GetMaxMinSum(out int max, out int min, out int sum, params int[] a)
        {
            max = -1;
            min = 100;
            sum = 0;
            for (int i = 0; i <a.Length; i++)
            {
                if (a[i] > max) max = a[i];
                if (a[i] < min) min = a[i];
                sum += a[i];
            }
        }
    }
}







派生由点线到矩形

结果如图:

代码:

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;

namespace 点线矩形派生
{
    class Program
    {
        static void Main(string[] args)
        {
            CRect cr = new CRect() { x1 = 0, y1 = 0, x2 = 2, y2 = 2 };
            Console.WriteLine("两点之间的距离len={0}",cr.GetLen());
            Console.WriteLine("矩形的周长perimeter={0}",cr.Perimeter());
            Console.WriteLine("矩形的面积是area={0}",cr.Area());
            Console.ReadKey();
        }
    }
    class CPoint
    {
        public double x1 { get; set; }
        public double y1 { get; set; }
        public double x2 { get; set; }
        public double y2 { get; set; }
    }
    class CLine:CPoint
    {
        public double GetLen()
        {
            double x;
            x = Math.Sqrt((x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2));
            return x;
        }
    }
    class CRect : CLine
    {
        public double Perimeter()
        {
            double x;
            x = 2 * (Math.Abs(x1 - x2) + Math.Abs(y1 - y2));
            return x;
        }
        public double Area()
        {
            double x;
            x = Math.Abs(x1 - x2) * Math.Abs(y1 - y2);
            return x;
        }
    }
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值