zoukankan      html  css  js  c++  java
  • 数组

    数组可以帮我们一次申请多个相同类型的变量,而且是连续存储的

    语法:

    数据类型【】 数组名=new 数据类型【数组长度】;

    例如:

    int[] score = new int[5];

    就声明了一个长度为5的数组,数组名叫score

    通俗点说就是声明了一个数组 里面包含5个int类型的变量

    数组名叫:score  里面的5个int类型的变量叫做:数组的元素

    如何访问数组 :通过下标(索引)来访问数组:数组名【编号】

    比如我样要向第0个元素  赋一个3.可以这样写:score[0]=3 



    int[] score = new int[5];
    score[0] = 10;
    score[4] = 20;
    Console.WriteLine(score[0]);
    Console.WriteLine(score[4]);
    Console.ReadKey();

    int[] scores= new int[5];//表示申请了1个int类型为scores的数组 长度为5
    scores[0]=50;//给数组中的第一个赋值为50
    Console.WriteLine("{0}",scores[0]);
    Console.ReadKey();


     int 数组一但声明,里面的每一个元素都被初始化成0

    int[] score = new int[5];
    score[0] = 10;
    score[4] = 20;
    Console.WriteLine(score[0]);
    Console.WriteLine(score[3]); //我们没有赋值但也会输出值:0  说明int 数组一但声明,里面的每一个元素都被初始化成0
    Console.WriteLine(score[4]);

    Console.ReadKey();


     通过 数组名.length 可以获得数组的长度

    Console.Clear();  清屏


    int[] score = new int[10];//数据类型【】 数组名=new 数据类型【数组长度】;

    int sum = 0;
    //对数组中的元素进行赋值
    for (int i = 0; i < score.Length; i++) //如何访问数组 :通过下标(索引)来访问数组:数组名【编号】
    {
    Console.WriteLine("请输入第{0}个人的成绩",i+1);
    score[i] = Convert.ToInt32(Console.ReadLine());
    //sum += score[0];//第一种写法

    }
    //通过一个循环求一个数组中所有元素的和
    for (int i = 0; i < score.Length; i++)
    {
    sum += score[i];
    }

    Console.Clear();//清屏
    Console.WriteLine("共多少{0}人的平均成绩为{1}",score.Length,sum/score.Length);

    //输出数组中第一个元素的值
    for (int i = 0; i < score.Length; i++)
    {
    Console.WriteLine("第{0}学生的成绩为{1}",i+1,score[i]);

    }


    Console.ReadKey();


    声明时直接赋值


    string[] names = {"zhangsan","lisi","wangww","lili" };
    for (int i = 0; i < names.Length;i++)

    {
    Console.WriteLine(names[i]);

    }
    Console.ReadKey();


    求最大值


    int[] score = new int[10];//数据类型【】 数组名=new 数据类型【数组长度】;
    int max;
    int sum = 0;
    //对数组中的元素进行赋值
    for (int i = 0; i < score.Length; i++) //如何访问数组 :通过下标(索引)来访问数组:数组名【编号】
    {
    Console.WriteLine("请输入第{0}个人的成绩", i + 1);
    score[i] = Convert.ToInt32(Console.ReadLine());
    //sum += score[0];//第一种写法

    }
    max = score[0];
    //求最大值
    for (int i = 0; i < score.Length;i++ )
    {
    if (score[i]>max)
    {
    max = score[1];
    }
    }
    //通过一个循环求一个数组中所有元素的和
    for (int i = 0; i < score.Length; i++)
    {
    sum += score[i];
    }

    Console.Clear();//清屏
    Console.WriteLine("共多少{0}人的平均成绩为{1}", score.Length, sum / score.Length);

    //输出数组中第一个元素的值
    for (int i = 0; i < score.Length; i++)
    {

    Console.WriteLine("第{0}学生的成绩为{1}", i + 1, score[i]);

    }

    Console.WriteLine("最大值{0}",max);
    Console.ReadKey();


    //第一题:求一组数的MAX MIN
    int[] number = { 3,5,8,9,90,120,85};
    int max = number[0];
    int min = number[0];
    for (int i = 1; i < number.Length;i++ ) //获取数组的长度

    {
    if(number[i]>max)
    {
    max = number[i];
    }
    if(number[i]<min)
    {
    min=number[i];
    }
    }
    Console.WriteLine("max={0} min={1}",max,min);
    Console.ReadKey();


    //第一题:求一组数的MAX MIN 求和
    int[] number = { 3,5,8,9,90,120,85};
    int max = number[0];
    int min = number[0];
    int sum = 0;
    for (int i = 1; i < number.Length;i++ ) //获取数组的长度

    {
    if(number[i]>max)
    {
    max = number[i];
    }
    if(number[i]<min)
    {
    min=number[i];
    }
    sum+=number[i];
    }
    Console.WriteLine("max={0} min={1} {2}",max,min,sum);
    Console.ReadKey();



    //第三题:最后一个不加分割线
    string[] names = { "梅西", "卡卡", "郑大世" };
    string str="";
    for(int i=0;i<names.Length;i++)
    {
    if (i == names.Length - 1)//表示到最后一个了
    {
    str = str + names[i];
    }
    else
    {

    str = str + names[i] + "|";
    }
    }

    Console.WriteLine(str);
    Console.ReadKey();


    //位置交换输出:
    string[] names = { "梅西", "卡卡", "郑大世","张三" };
    string temp;
    for (int i = 0; i < names.Length / 2; i++)
    {
    temp = names[i];
    names[i] = names[names.Length - 1 - i];
    names[names.Length - 1 - i] = temp;
    }
    for (int i = 0; i < names.Length; i++)
    {
    Console.Write(names[i]);
    }
    Console.ReadKey();


    //第四题:位置颠倒输出:

    string[] names = { "梅西", "卡卡", "郑大世","张三" };
    for (int i = names.Length - 1; i >= 0; i--)
    {
    Console.Write(names[i]);
    }
    Console.ReadKey();

  • 相关阅读:
    asp.net 2.0 run
    Regular Expression
    assembly
    asp.net loading..
    session
    asp.net performance
    asp.net page order
    interface
    UVA 562 Dividing coins
    UVA 10003 Cutting Sticks
  • 原文地址:https://www.cnblogs.com/swlq/p/5374497.html
Copyright © 2011-2022 走看看