C#基础05

这篇博客深入探讨了C#编程语言的基础知识,包括变量、数据类型、控制流程和函数的使用。通过实例解析,读者将更好地理解C#编程的核心概念。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

            //数据类型[] 数组名 = new 数据类型[数组长度];  指定数组的长度
            //int[] ints = new int[5];
            //数组索引 从0开始  对数组的数组元素进行复制 从0开始
            //ints[1] = 1;
            //ints[2] = 2;
            //ints[3] = 3;
            //ints[4] = 4;
            //ints[5] = 5;
            //数据类型[] 数组名 = { 值1,值2,值3,值4 };  由内容来指定数组的长度
            //int[] ints = { 1, 2, 3, 4, 5 };
            //Console.WriteLine(ints.Length);

            //数据类型[,] 数组名 = new 数据类型[数值1, 数值2];
            //所有的数据类型 都会有默认值  值类型  关键字  蓝色
            //int[,] ints = new int[5, 10];
            //ints[0, 0] = 1;
            //ints[0, 1] = 2;
            //Console.WriteLine(ints[3,4]);

            //数据类型[,] 数组名 = new 数据类型[,] { { 值1,值2 }, { 值1,值2 }, { 值1,值2 } };
            //int[,] ints = new int[,] { { 1, 1 }, { 2, 2 }, { 3, 3 }, { 4, 4 } };
            //Console.WriteLine(ints[3,1]);
            //Console.WriteLine(ints.Length);



            //遍历数组
            //for  是根据下标来进行获取值的操作
            //int[] ints = { 100, 200, 300, 400, 500 };
            //for (int i = 0; i < ints.Length; i++)
            //{
            //    Console.WriteLine(ints[i]);
            //}
            //Console.WriteLine("---------------------------------------");
            //foreach(元素类型  item  in  集合/数组名称)
            //foreach (int item in ints)
            //{
            //    Console.WriteLine(item);
            //}

            //删除数组中的元素
            //int[] ints = { 100, 200, 300, 400, 500 };
            //Console.WriteLine("请输入你要删除数组的第几个元素的下标");
            //int index = int.Parse(Console.ReadLine());
            //for (int i = index; i < ints.Length - index; i++)
            //{
            //    ints[i] = ints[i + 1];
            //}
            ////将最后一位数字替换为0
            ////ints[index] = 0; //----- ① 
            //ints[ints.Length - 1] = 0;
            //Console.WriteLine("----------------------------------------");
            //foreach (int item in ints)
            //{
            //    Console.WriteLine(item);
            //}


            //冒泡排序  红黑树
            //int[] ints = { 700, 200, 300, 400, 500, 600 };
            //for (int i = 0; i < ints.Length - 1; i++)
            //{
            //    for (int j = 0; j < ints.Length - 1 - i; j++)
            //    {
            //        if (ints[j] > ints[j + 1])
            //        {
            //            int temp = ints[j];
            //            ints[j] = ints[j + 1];
            //            ints[j + 1] = temp;
            //        }
            //    }
            //}
            ////遍历数组
            //foreach (int item in ints)
            //{
            //    Console.WriteLine(item);
            //}

            //Sort方法用于对一维Array数组中的元素进行排序。
            //从小到大
            //int[] ints = { 700, 200, 300, 50, 30, 600 };
            //Array.Sort(ints);
            //Console.WriteLine("--------------");
            //// 遍历数组
            //foreach (int item in ints)
            //{
            //    Console.WriteLine(item);
            //}
            // Reverse方法用于反转一维数组元素的顺序。但是不会进行排序。
            //先排序 后反转 从大到小
            //int[] ints = { 700, 200, 300, 50, 30, 600 };
            //Array.Sort(ints);
            //Array.Reverse(ints);
            //Console.WriteLine("--------------");
            //// 遍历数组
            //foreach (int item in ints)
            //{
            //    Console.WriteLine(item);
            //}


            //合并数组
            //int[] array1 = { 1, 20, 3, 40, 5 };
            //int[] array2 = { 60, 7, 8, 90, 10, };
            //int[] array3 = new int[array1.Length + array2.Length];
            //for (int i = 0; i < array3.Length; i++)
            //{
            //    if (i < array1.Length)
            //    {
            //        array3[i] = array1[i];
            //    }
            //    else
            //    {
            //        array3[i] = array2[i - array1.Length];
            //    }
            //}
            ////遍历数组
            //foreach (int item in array3)
            //{
            //    Console.WriteLine(item);
            //}

            //拆分数组
            //分配 基数拆分
            //int[] ints = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13 };
            //int index = 0;
            //if (ints.Length % 2 != 0)
            //{
            //    index = ints.Length % 2;
            //}
            //int[] int1 = new int[ints.Length / 2];
            //int[] int2;
            //if (index > 0)
            //{
            //    int2 = new int[(ints.Length / 2) + index];
            //}
            //else
            //{
            //    int2 = new int[ints.Length / 2];
            //}
            ////拆
            //for (int i = 0; i < ints.Length; i++)
            //{
            //    // 0,1,2,3,4,5
            //    if (i < int1.Length)
            //    {
            //        int1[i] = ints[i];
            //    }
            //    else
            //    {
            //        // 6,7,8,9,10,11,12
            //        int2[i - int1.Length] = ints[i];
            //    }
            //}
            ////遍历数组
            //Console.WriteLine("-----------------int1");
            //foreach (int item in int1)
            //{
            //    Console.WriteLine(item);
            //}
            ////遍历数组
            //Console.WriteLine("-----------------int2");
            //foreach (int item in int2)
            //{
            //    Console.WriteLine(item);
            //}



            //② 整数拆分
            //int[] array1 = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12};
            //int[] array2 = new int[array1.Length / 2];
            //int[] array3 = new int[array1.Length / 2];
            //for (int i = 0; i < array1.Length; i++)
            //{
            //    if (i < 5)
            //    {
            //        array2[i] = array1[i];
            //    }
            //    else
            //    {
            //        array3[i - 5] = array1[i];
            //    }
            //}
            ////遍历数组
            //foreach (int item in array3)
            //{
            //    Console.WriteLine(item);
            //}



            //1.定义一个长度为5的字符串数组。循环输入5个姓名。然后再输入一个用户的姓名,
            //	检查数组中是否有这个人的姓名,并输出所在下标。
            //string[] str = { "aa", "bb", "cc", "dd", "ee" };
            //Console.WriteLine("输入一个用户的姓名");
            //string s = Console.ReadLine();
            //for (int i = 0; i < str.Length; i++)
            //{
            //    if (s.Equals(str[i]))
            //    {
            //        Console.WriteLine(i);
            //    }
            //}



            //2.定义一个长度为5的整数数组,循环赋值,根据用户输入的位数,进行对应的删除
            //int[] ints = { 100, 200, 300, 400, 500 };
            //Console.WriteLine("请输入你要删除数组的第几个元素的下标");
            //int index = int.Parse(Console.ReadLine());
            //for (int i = index; i < ints.Length - index; i++)
            //{
            //    ints[i] = ints[i + 1];
            //}
            ////将最后一位数字替换为0
            //ints[index] = 0; //----- ① 
            //// ints[ints.Length - 1] = 0;
            //foreach (int item in ints)
            //{
            //    Console.WriteLine(item);
            //}


            //3.定义一个长度为7的字符串数组,并为其赋值,将该数组的值进行反转
            //int[] ints = { 700, 200, 300, 50, 30, 600 };
            //Array.Sort(ints);//排序
            //Array.Reverse(ints);//反转
            //// 遍历数组
            //foreach (int item in ints)
            //{
            //    Console.WriteLine(item);
            //}

            //4.99乘法表
            //int max = 9;
            //int count = 1;
            //while (count <= max)
            //{
            //    for (int i = 1; i <= count; i++)
            //    {
            //        Console.Write(i+"*"+count+"="+i*count);
            //        Console.Write("\t");
            //    }
            //    Console.Write("\n");
            //    count = count + 1;
            //}


            //5.冒泡循环
            //int[] ints = { 2, 1, 33, 4, 22, };
            //for (int i = 1; i < ints.Length - 1; i++)
            //{
            //    for (int j = 0; j < ints.Length - i - 1; j++)
            //    {
            //        if (ints[j] > ints[j + 1])
            //        {
            //            int temp = ints[j];
            //            ints[j] = ints[j + 1];
            //            ints[j + 1] = temp;
            //        }

            //    }
            //}
            //foreach (int item in ints)
            //{
            //    Console.WriteLine(item);
            //}

            //6.二维数组的一个棋盘
           

           // Console.ReadKey();

        }
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值