zoukankan      html  css  js  c++  java
  • 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();

     

  • 相关阅读:
    轻松实现 网络视频播放器
    端口扫描 多方式协同实现
    关于部分应用无法向POJ提交代码的解决方案
    一套跨平台五子棋网游的开发经历
    SSH2.0编程 ssh协议过程实现
    ssh秘钥交换详解与实现 diffie-hellman-group-exchange-sha
    OnlineJudge 离线题库采集
    SOCKET 实现NAT 穿越
    基于公网smtp协议实现邮件服务器
    周六搞事情,微信小程序开发文档已放出!
  • 原文地址:https://www.cnblogs.com/xinghun/p/5269301.html
Copyright © 2011-2022 走看看