0915,数组,练习题,

数组: 有固定长度的同种类型的一组变量,有索引,索引从0开始

foreach 便利数组

int[] shuzu = new int[6]; // 定义了一个长度为6的int类型的数组            

Random r = new Random();

for (int i = 0; i < 6; i++)            

{

    shuzu[i] = r.Next(1,56);

    Console.WriteLine(shuzu[i]);                        

}

 

      // 一, 如何生成 6个不一样的数

 

int[] shuzu = new int[6]; //循环生产六个数            

Random r = new Random();

           

for (int i = 0; i < 6; i++)            

{            

    shuzu[i] = r.Next(1, 34); // 生产一个数

           

    bool isok = false;            

    for (int j = 0; j < i; j++)

 比较是否跟之前的数相等            

    {            

         if (shuzu[j] == shuzu[i])            

        {            

             isok = true;            

        }            

    }

           

     if (isok)            

    {

           

        i--;

 后退一步            

        continue;            

    }            

    Console.WriteLine(shuzu[i]);            

}            

shuzu[6] = r.Next(1,17);

 

      二,// 你输入一个号码  跟电脑随机生成的号码去比对  

 

Console.Write("请输入你所选的号码:");            

 int a = int.Parse(Console.ReadLine());                        

int[] shuzu = new int[7];            

 Random r = new Random();

           

for (int d = 0; d <=7; d++)            

{            

    int dn = r.Next(1, 32);

           

    bool isok = false;            

    for (int j = 0; j < d; j++)            

     {            

        if (shuzu[j] == shuzu[d])            

        {            

            isok = true;             

         }                                    

    }            

     if (isok)            

    {            

        d--;            

         continue;            

     }            

    Console.WriteLine("电脑显示的七位数是",shuzu[d]);            

}

 

            三 ,//输入10个人的分数,求最高分最低分平均分            

Console.Write("请输入10个人的分数:");

           

 int[] chengji = new int[10];

           

int max = 0;            

int min = 0;            

int sum = 0;            

for (int i = 0; i < 10; i++)            

 {            

     chengji[i] = int.Parse(Console.ReadLine());            

     sum += chengji[i];            

     if (max < chengji[i])            

    {            

        max = chengji[i];            

    }            

    if (min > chengji[i])            

    {            

        min = chengji[i];            

    }            

}            

Console.WriteLine("最大值" + max);            

 Console.WriteLine("最小值" + min);            

Console.WriteLine("平均值" + sum / 10);

 

posted @ 2015-09-17 14:46  jlhea  阅读(119)  评论(0编辑  收藏  举报