• 3.12课·········数组


    数组:具有相同类型的若干变量按有序的形式组织起来的一种形式。这些按序排列的同类数据元素的集合称为数组。
    定义数组
    int[] 变量名 = new int [n];

    string[] myStringArray = new string[6];

    int[] myArray = new int[] {1, 3, 5, 7, 9};

    数组索引号从0开始
    一维数组[相同数据类型]
    第一种定义方式
    int[] shuzu=new int[5];
    for (int i = 0; i <= 5;i++ )//用for循环为shuzu赋值
    {
    shuzu[i] = i + 1;
    }
    索引从0开始
    赋值
    shuzu[0] = 1;
    shuzu[1] = 2;
    shuzu[2] = 3;
    shuzu[3] = 4;
    shuzu[4] = 5;

    第二种定义方式
    int[] shuzu=new int[]{1,2,3,4,5};

    Console.WriteLine(shuzu[2]);//3

    输入10个人的年龄,并求和

    int[] age = new int[10];
                int sum = 0;
                for (int i = 0; i < 10; i++)
                {
                    Console.Write("请输入第" + (i + 1) + "个人的年龄:");
                    age[i]=int.Parse(Console.ReadLine());//获取这个数并转换成数值类型放进数组age
                    sum += age[i];
                }
                Console.WriteLine("这10个人的年龄之和是" + sum);
                Console.ReadLine();

     输入人名放进数组,输出第五个人的名字

        string[] s = new string[10];
                for (int i = 0; i < 10;i++ )
                {
                    Console.Write("请输入第"+(i+1)+"个人的名字:");
                    s[i] = Console.ReadLine();
                }
                Console.Write("第五个人的名字是"+s[4]);
                Console.ReadLine();

     输入班级人数,根据人数挨个输入成绩,求平均分

        Console.Write("请输入班级人数:");
                int a = int.Parse(Console.ReadLine());
                dou
                double[] chengji = new double[a];
                double sum = 0;
                for (int i = 0; i < a; i++)
                {
                    Console.Write("请输入第" + (i + 1) + "个人的成绩:");
                    chengji[i] = double.Parse(Console.ReadLine());
                    sum += chengji[i];
                }
                Console.WriteLine("" + a + "个人的平均分是" + sum / a); 

    二维数组
    int[,] shuzu = new int[3, 4];
    int[,] shuzu = new int[,] {
    {1,2,3,4},
    {5,6,7,8},
    {6,7,8,9}
    };

    输入班级人数,将每个人的
    语文,数学,英语成绩输入二维数组

                Console.Write("请输入班级人数:");
                int a = int.Parse(Console.ReadLine());
                double [,] shuzu = new double [a,3];
    
                for (int i = 0; i < a; i++)
                {
                    for (int j = 0; j < 3; j++)
                    {
                        if (j == 0)
                        {
                            Console.Write("请输入第"+(i+1)+"个人的语文成绩:");
                        }
                        if (j == 1)
                        {
                            Console.Write("请输入第" + (i + 1) + "个人的数学成绩:");
                        }
                        if (j == 2)
                        {
                            Console.Write("请输入第" + (i + 1) + "个人的英语成绩:");
                        }
                        shuzu[i, j] = double.Parse(Console.ReadLine());
                    }
                }
                    Console.ReadLine();

    打印字:王,企:

    string[,] shuzu = new string[,] {
    {" ","■","■","■","■","■",""},
    {" "," "," ","■","","",""},
    {" "," "," ","■","","",""},
    {" "," ","■","■","■","",""},
    {" "," "," ","■"," "," ",""},
    {" "," "," ","■"," "," ",""},
    {"■","■","■","■","■","■","■"}
    };

                for (int i = 0; i < 7; i++)
                {
                    for (int j = 0; j < 7; j++)
                    {
                        Console.Write(shuzu[i, j]);
                    }
                    Console.WriteLine();
                } 

    string[,] s = new string[,]{
    {" "," "," ","●"," "," "," "},
    {" "," ","●"," ","●"," "," "},
    {" ","●"," ","●"," ","●"," "},
    {"●"," "," ","●"," "," ","●"},
    {" ","●"," ","●","●","●"," "},
    {" ","●"," ","●"," " ," "," "},
    {"●","●","●","●","●","●","●"}
    };

                for (int i = 0; i < 7; i++)
                {
                    for (int j = 0; j < 7; j++)
                    {
    
                        Console.Write(s[i, j]);
    
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();

     

  • 相关阅读:
    java.lang.IllegalArgumentException: Not an managed type
    时间 日期 转换
    纯JavaScript开发飞机大战项目
    【thinkPHP框架】Failed opening required 'header.php' include_path='.;c:php5pear 终级解决方案
    【精编重制版】JavaWeb 入门级项目实战 -- 文章发布系统 (第二节)
    【PHP快速入门】 第二节 php基本语法
    【精编重制版】JavaWeb 入门级项目实战 -- 文章发布系统 (第一节)
    个人博客恢复访问
    【H5 音乐播放实例】第一节 音乐详情页制作(1)
    【php增删改查实例】第二十六节
  • 原文地址:https://www.cnblogs.com/xinghun/p/5269301.html
Copyright © 2020-2023  润新知