C# 入门小练习(持续更新)

来源:互联网 发布:怎么看自己淘宝店铺号 编辑:程序博客网 时间:2024/06/11 18:48


    #region 计算任意多个数间的最大值    static void Main(string[] args)    {List<int> Nums = new List<int> { 10, 250, 33, 21, 54, 102 };        int max = GetMaxNum(Nums);        Console.WriteLine("最大值是:{0}", max);        Console.ReadKey();    }    private static int GetMaxNum(List<int> s)    {        int max = s[0];        for (int i = 1; i < s.Count; i++)        {             if (s[i] > max)             {                 max = s[i];             }        }        return max;    }    #endregion
 #region 显示1-100之间的素数,并显示出出一共有多少个素数,并计算出所有素数之和            string Sushu = "1";            int count = 1;            int sushu = 1;            while(true)            {                   for(int i=2;i<=100;i++)                      {                    bool zhishu = true;      //是否为素数的布尔值默认为true                    for (int j=2;j<i;j++)                        {                        if (i % j == 0)                        {                            zhishu = false;     //判断是否为素数,如果不为素数,布尔值为false,并跳出循环                            break;                              }                    }                    if (zhishu)                    {                        Sushu = Sushu + "," + Convert.ToString(i);                        count++;                        sushu += i;                    }                }                Console.WriteLine("1-100之间的素数有:{0}",Sushu);                Console.WriteLine("素数的个数是:{0}", count);                Console.WriteLine("所有的素数之和为:{0}",sushu);                Console.ReadKey();            }      #endregion