C#基础代码笔记(三)

前言:

该博客将介绍一些数组的相关基础练习代码,以及函数方法基础代码。

相关资料:【01_C#入门到精通】新手强烈推荐:C#开发课程,一整套课程


数组

1.数组的遍历输出

static void Main(string[] args)
        {
            int[] nums = new int[10];
            for(int i=0;i<nums.Length;i++)
            {
                nums[i] = i;
            }
            for(int i=0;i<nums.Length;i++)
            {
                Console.WriteLine(nums[i]);
            }
            Console.ReadKey();    
        }

2.数组的最大值,最小值,总和与平均数

static void Main(string[] args)
{
    int[] nums = {1,2,3,4,5,6,7,8,9,0};
    int max = nums[0];
    int min = nums[0];
    int sum = 0;
    //循环的让数组中的每个元素跟我的最大值,最小值进行比较

    for(int i=0;i<nums.Length;i++)
    {
        //关于在循环中nums[i]的理解方式
        //1.代表数组中当前循环到的元素
        //2.代表数组中的每个元素
        //如果数组中当前的循环到的这个元素,比我的max还要大,则把当前这个元素赋给我的max
        if(nums[i]>max)
        {
            max = nums[i];
        }
        if(nums[i]<min)
        {
            min = nums[i];
        }
        sum += nums[i]; 
    }
    Console.WriteLine("这个数组的最大值是{0},最小值是{1},总和是{2},平均值是{3}", max, min, sum, sum / nums.Length);
    Console.ReadKey();
}

3.数组与“+”号的练习

static void Main(string[] args)
        {
            string[] names = { "小马", "小苏", "小虎","小牛","小周" };
            //小马|小苏|小虎|小牛|小周
            //解题思路:通过一个循环,获得字符串数组中的每一个元素。
            //然后,将这个每一个元素都累加到一个字符串中,以|分割
            string str = null;
            for(int i=0;i<names.Length-1;i++)
            {
                str += names[i] + "|";
            }
            Console.WriteLine(str+names[names.Length-1]);
            Console.ReadKey();

        }

4.数组改变元素值

static void Main(string[] args)
{
    int[] nums = { 1, -2, 3, 4, 5, 0 };
    //解题思路:通过一个循环,获得数组中的每一个元素
    //对每一个元素进行判断
    for(int i=0;i<nums.Length;i++)
    {
        if(nums[i]>0)
        {
            nums[i] += 1;
        }
        if(nums[i]<0)
        {
            nums[i] -= 1;
        }
        else
        {

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

5.数组颠倒输出

static void Main(string[] args)
{
    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 - i - 1] = temp;
    }
    for (int i = 0; i < names.Length; i++)
    {
        Console.Write(names[i]);
    }
    Console.ReadKey();
}

6.冒泡排序

static void Main(string[] args)
{
    int[] nums = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 };
    for (int i = 0; i < nums.Length - 1; i++)
    {
        for (int j = 0; j < nums.Length - i - 1; j++)
        {
            if (nums[j] < nums[j + 1])
            {
                int temp = nums[j];
                nums[j] = nums[j + 1];
                nums[j + 1] = temp;
            }
        }
    }
    for (int i = 0; i < nums.Length; i++)
    {
        Console.WriteLine(nums[i]);
    }


}

基础方法(函数)

1.输出最大值(方法一)

class Program
{
    static void Main(string[] args)
    {
        //计算两个整数之间的最大值
        int max=Program.Getmax( 1,3);
        Console.WriteLine(max);


    }
    public static int Getmax(int n1,int n2)
    {//计算两个整数之间的最大值并且将返回最大值
        return n1 > n2 ? n1 : n2;
    }
}

2.判断闰年

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

            //举例:写一个方法,判断一个年份是否是闰年
            bool a=isRun(2000);
            Console.WriteLine(a);
        }

        /// <summary>
        /// 判断给定的年份是否是闰年
        /// </summary>
        /// <param name="year">要判断的年份</param>
        /// <returns>是否是闰年</returns>
        public static bool isRun(int year)
        {
            bool b = (year%4==100)||(year%4==0&&year%100!=0);
            return b;
        }
   
    }

3.输出最大值(方法二)

static void Main(string[] args)
    {

        //比较两个数字的大小 返回最大的
        int a = 10;
        int b = 10;
        int temp=GetMax(a,b);//实参
        Console.WriteLine(temp);
    }

    /// <summary>
    /// 计算两个整数之间的最大值 并且返回最大值
    /// </summary>
    /// <param name="n1">第一个数</param>
    /// <param name="n2">第二个数</param>
    /// <returns>返回的最大值</returns>
    public static int GetMax(int n1, int n2)//形参
    {
        int max = n1 > n2 ? n1 : n2;
        return max;
    }

4.读取正确的整数

static void Main(string[] args)
        {
            //1.读取输入的整数
            //2.多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入)
            Console.WriteLine("请输入一个数字");
            string input = Console.ReadLine();
            int number = GetNumber(input);
            Console.WriteLine(number);
        }

        /// <summary>
        /// 这个方法判断用户的输入是否是数字
        /// 如果是数字,则返回
        /// 如果不是数字,提示用户重新输入
        /// </summary>
        public static int GetNumber(string s)
        {
            while(true)
            {
                try
                {
                    int number = Convert.ToInt32(s);
                    return number;
                }
                catch
                {
                    Console.WriteLine("请重新输入");
                    s = Console.ReadLine();
                }
            }
        }

5.用户登录

static void Main(string[] args)
{
    //2.只允许用户输入y或n,请该层方法
    //这个方法做了什么事呢?
    //只能让用户输入yes或者no,只要不是,就重新输入
    Console.WriteLine("请输入y或n");
    string input = Console.ReadLine();
    insert(input);

}

public static void insert(string s)
{
    while (true)
    {
        try
        {
            if ((s == "y") || (s == "n"))
            {
                Console.WriteLine("登录成功");
                break;
            }
            else
            {
                Console.WriteLine("输入错误,请重新输入");
                s = Console.ReadLine();
            }
        }
        catch
        {
            Console.WriteLine("输入错误,请重新输入");
            s = Console.ReadLine();
        }
    }

6.求数组总和

 static void Main(string[] args)
        {
            int[] nums = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 0 };
            int sum = Getsum(nums);
            Console.WriteLine(sum);
            Console.ReadKey();
        }

        /// <summary>
        ///计算一个整数类型数组的总和
        /// </summary>
        /// <param name="nums">要求总和的数组</param>
        /// <returns>返回这个数组的总和</returns>
        public static int Getsum(int[] nums)
        {
            int sum = 0;
            for(int i=0;i<nums.Length;i++)
            {
                sum += nums[i];
            }
            return sum;
        }

7.多个返回值放在数组中返回

//函数返回数组		
		static void Main(string[] args)
        {
            //写一个方法 求一个数组中的最大值,最小值,总和,平均值
            int[] numbers = {1,2,3,4,5,6,7,8,9 };
            //将要返回的4个值,放到一个数组中返回

            int[] res = GetMaxMinSumAvg(numbers);
            Console.WriteLine("{0}是最大值,{1}是最小值,{2}是总和,平均值{3}",res[0],res[1],res[2],res[3]);
        }
        /// <summary>
        /// 
        /// </summary>
        /// 计算一个数组的最大值,最小值,总和和平均值
        /// <param name="nums"></param>
        /// <returns></returns>
        public static int[] GetMaxMinSumAvg(int[] nums)
        {
            int[] res = new int[4];
            //假设 res[0] 最大值 res[1]最小值 res[2]总和 res[3]平均值
            res[0] = nums[0];
            res[1] = nums[0];
            res[2] = 0;
            for(int i=0;i<nums.Length;i++)
            {
                //如果当前循环到的元素比我假定的最大值还大
                if(nums[i]>res[0])
                {
                    //将当前循环到的元素赋值给我的最大值
                    res[0] = nums[i];
                }
                if(nums[i]<res[1])
                {
                    res[1] = nums[i];
                }
                res[2] += nums[i];
            }
            res[3] = res[2] / nums.Length;
            return res;
        }
  • 3
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论
#region 1:一列数的规则如下: 1、1、2、3、5、8、13、21、34......求第30位数是多少, 用递归算法实现。 //int n = FindNum(30); //Console.WriteLine(n); //Console.ReadKey(); #endregion #region 2:有一个3*4矩阵,输出最大元素的值,及其所在的行号和列号, int a[3][4]={{1,2,3,4},{9,8,7,6}, {-10,10,-5,2}}。 //int[,] a = { { 1, 2, 3, 4 }, { 9, 8, 7, 6 }, { -10, 10, -5, 2 } }; //Max1(a); //Console.ReadKey(); #endregion #region 3:实现二分法查找,int a[8] = {3,12,24,36,55,68,75,88},查找24需要几次查找出来。 //int[] a = { 3,12,24,36,55,68,75,88 }; //int value = 24; //Select(a, value); //Console.ReadKey(); #endregion #region 4:实现冒泡排序, int[] array = { 23, 45, 16, 7, 42 }。 //int[] array = { 23, 45, 16, 7, 42 }; //Sort(array); //foreach(int n in array) //{ // Console.WriteLine(n); //} //Console.ReadKey(); #endregion #region 5:求出0-1000中能被7整除的数,并计算输出每五个数的和。 //int sum = 0;//每五个数的和 //int count = 0;//计算个数 //for(int i=1;i<=1000;i++) //{ // if(i%7==0) // { // count++;//个数加一 // sum = sum + i; // if (count % 5 == 0) // { // Console.WriteLine(sum); // sum = 0; // } // } //} //Console.WriteLine("1 - 1000中一共有{0}个数被7整除", count); //Console.ReadKey(); #endregion #region 6:编写一个类,其中包含一个排序的方法 Sort(), 当传入的是一串整数,就按照从小到大的顺序输出 如果传入的是一个字符串,就将字符串反序输出。 //SortAndReverse sa = new SortAndReverse(); //string intput = "woaini"; //Console.WriteLine(sa.Sort(intput)); //Console.ReadKey(); #endregion #region 7:编写一个矩形类,私有数据成员为矩形的长(len)和宽(wid),无参构造函数将len和wid设置为0,有参构造函数设置len和wid的值;另外,类还包括矩形的求周长、求面积、取矩形的长度、取矩形的宽度、修改矩形的长度和宽度为对应的形参值等公用方法。 //Rectangular rt = new Rectangular(); //rt.Len = 10; //rt.Wid = 15; //Console.WriteLine(rt.Area()); //Console.ReadKey(); #endregion #region 8:编写一个控制台应用程序,接收一个长度大于3的字符串,完成 下列功能:1 输出字符串长度。  2 输出字符串中第一个出现字母a的位置。  3 在字符串的第3个字符后面插入子串“hello”,输出新字符串。 4 将字符串“hello”替换成“me”,输出新字符串。  5 以字符“m”为分隔符,将字符串分离,并输出分离后的字符串。) //Console.WriteLine("请输入一个长度大于3的字符串:"); //string str= Console.ReadLine(); ////输出字符串长度 //Console.WriteLine("字符串长度为:{0}", str.Length); ////输出字符串中第一个出现字母a的位置 //if (str.IndexOf("a") == -1) //{ // Console.WriteLine("未找到字母a"); //} //else //{ // Console.WriteLine("第一个出现字母a的位置是{0}", str.IndexOf("a")); //} ////在字符串的第3个字符后面插入子串“hello”,输出新字符串。 //str = str.Insert(3, "hello"); //Console.WriteLine(str); ////将字符串“hello”替换成“me”,输出新字符串。 //str = str.Replace("hello", "me"); //Console.WriteLine(str); ////以字符“m”为分隔符,将字符串分离,并输出分离后的字符串 //string[] str1 = str.Split('m'); //Console.Write("分离后的字符串为:"); //foreach(string n in str1) //{ // Console.Write(n + " "); //} //Console.ReadKey(); #endregion

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

柠檬茶12138

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值