码迷,mamicode.com
首页 > 编程语言 > 详细

一维数组

时间:2016-10-14 23:12:28      阅读:254      评论:0      收藏:0      [点我收藏+]

标签:

数组:相同数据类型的元素按照一定的顺序进行排列生成的集合一组数据

一维数组:
int [] array=new int[5];
int[] array = new int[] {1,2,3,4,5 };
int[] array = new int[5] { 1, 2, 3, 4, 5 };

定义的时候,需要数据类型、能够存放元素的数量
int[] array = new int[5];
//按照顺序:索引 从0开始
array[0] = 3;
array[1] = 3;
array[2] = 3;
array[3] = 3;
array[4] = 3;
array[5] = 3;错误,索引号超出界限

请输入班级人数,存放一下每个人的姓名,打印输出

 1 //Console.Write("请输入班级人数:");
 2             //int n = int.Parse(Console.ReadLine());
 3             //string[] name = new string[n];
 4             //for (int i = 0; i < name.Length; i++)
 5             //{
 6             //    Console.Write("请输入第{0}个姓名:", (i + 1));
 7             //    name[i] = Console.ReadLine();
 8             //}
 9             //Console.WriteLine("所有人员姓名输入完毕,请按回车键进行人员姓名打印!");
10             //Console.ReadLine();
11             //Console.Clear();//清屏
12             ////for (int i = 0; i < n;i++ )
13             ////{
14             ////    Console.WriteLine("第{0}个姓名:{1}",(i+1),name[i]);
15             ////}
16             ////遍历集合或数组
17             //foreach (string aa in name)
18             //{
19             //    Console.WriteLine(aa);
20             //}
请输入班级人数,输入每个人的年龄,将所有人的年龄进行排序
 1 //Console.Write("请输入班级人数;");
 2             //int n = int.Parse(Console.ReadLine());
 3             //string[] name = new string[n];
 4             //for (int i = 0; i < name.Length; i++)
 5             //{
 6             //    Console.Write("请输入第{0}个姓名", (i + 1));
 7             //    name[i] = Console.ReadLine();
 8             //}
 9             //Console.WriteLine("所有人员姓名输入完毕请按回车键继续");
10             //Console.ReadLine();
11             //Console.Clear();
12             //for (int i = 0; i < n; i++)
13             //{
14             //    Console.WriteLine("第{0}个姓名{1}", (i + 1), name[i]);
15             //}
16 
17             // Console.Write("请输入班级人数");
18             // int a = int.Parse(Console.ReadLine());
19             // int[] name=new int[a];
20             // for (int i = 0; i < a;i++ )
21             // {
22             //     Console.Write("请输入第{0}个年龄",(i+1));
23             //     name[i]=int.Parse(Console.ReadLine());
24             // }
25             // Console.WriteLine("所有人员姓名输入完毕,请按回车键继续");
26             // Console.ReadLine();
27             // Console.WriteLine("以下打印的是原来的顺序");
28             // foreach(int aa in name)
29             // {
30             //     Console.WriteLine(aa);
31             // }
32             // Console.WriteLine("请按回车键进行排序");
33             // Console.ReadLine();
34             // for (int i = 0; i < a - 1;i++ )
35             // {
36             //     for (int j = i +
37             //         1; j < a;j++ )
38             //     {
39             //         if(name[i]<name[j])
40             //         {
41             //             int zhong=name[i];
42             //             name[i] = name[j];
43             //             name[j]=zhong;
44             //         }
45             //     }
46             // }
47             // Console.WriteLine("以下打印的是排序之后的姓名");
48             //foreach(int aa in name)
49             //{
50             //    Console.WriteLine(aa);
51             //}
随机生成10个不重复的50以内的整数
/Random ran = new Random();
            //int[] x = new int[10];
            //for (int i = 0; i < 10; i++)
            //{
            //    x[i] = ran.Next(0, 10);
            //    for (int j = 0; j < i; j++)
            //    {
            //        if (x[i] == x[j])
            //        {
            //            x[i] = ran.Next(0, 10);
            //            j = -1;
            //        }
            //    }
            //}
            //foreach (int a in x)
            //{ Console.Write(a + "\t"); }

 

输入班级人数,输入每个人的分数,求班级最高分,最低分,平均分,去掉两个最高分并且去掉两个最低分之后的所有人员的平均分。
//Console.Write("请输入人数");
            //int renshu = int.Parse(Console.ReadLine());
            //int[] chengji = new int[renshu];
            //if (renshu >= 5)
            //{
            //    for (int h = 1; h < renshu; h++)
            //    {
            //        Console.Write("请输入第" + h + "个人的成绩");
            //        chengji[h - 1] = int.Parse(Console.ReadLine());
            //    }
            //    for (int i = 0; i < renshu; i++)
            //    {
            //        for (int j = i; j < renshu - 1; j++)
            //        {
            //            if (chengji[i] < chengji[j + 1])
            //            {
            //                int zh = 0;
            //                zh = chengji[i];
            //                chengji[i] = chengji[j + 1];
            //                chengji[j + 1] = zh;
            //            }
            //        }
            //    }
            //    int he = 0;
            //    for (int i = 0; i <= (renshu - 1); i++)
            //    {
            //        he += chengji[i];
            //    }
            //    double pj = (he - chengji[0] - chengji[1] - chengji[renshu - 2] - chengji[renshu - 1]) / (renshu - 4);
            //    Console.WriteLine("去掉两个最高分{0}、{1},去掉两个最低分{2}、{3},最后平均成绩是:{4}", chengji[0], chengji[1], chengji[renshu - 2], chengji[renshu - 1], pj);
            //}
            //else
            //{
            //    Console.WriteLine("输入人数要大于10");
            //}

随机生成一注彩票中奖号码。(6个红球(范围1~33),一个蓝球(1~16)),注意,不能重复

 //int[]a=new int[6];
            //Random ran = new Random();
            //Console.Write("红球为:");
            //for (int i = 0; i < 6;i++ )
            //{
            //    a[i]=ran.Next(1,34);
            //    for(int j=0;j<i;j++)
            //    {
            //        if(a[i]==a[j])
            //        {
            //            a[i]=ran.Next(1,34);
            //            j=-1;
            //        }
            //    }
            //    Console.WriteLine(a[i]+"");
            //}
            //int bule=ran.Next(1,17);
            //Console.WriteLine("蓝球为"+bule);

  


 
 

一维数组

标签:

原文地址:http://www.cnblogs.com/1030351096zzz/p/5962154.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!