Unity3D开发-C#语言进阶篇(if与for和while之间应用)

 class Program
    {
        static void Main(string[] args)
        {
            diyiti();
            dierti();
            disanti();
            disiti();
            diwuti();
            diliuti();
            diqiti();
            dibati();
            Console.ReadKey();
        }

        static void diyiti()
        {
            //------------第一题----------
            int js = 0;//计数;
            int num = 0;
            for (int i = 1; i < 1000; i++)
            {
                if (i % 7 == 0 || i % 10 == 7 || i / 10 % 10 == 7 || i / 100 == 7)
                {
                    Console.WriteLine("这些数分别是{0}\n\n", i);
                    js++;
                    num += i;
                }

            }

            Console.WriteLine("第一题个数{0},和{1}\n\n", js, num);


        }

        static void dierti()
        {
            //------------第二题----------
            double num = 0;
            for (double i = 1; i <= 100; i++)
            {
                num += 1 / i;//整形除整形还是整形
            }
            Console.WriteLine("第二题{0}\n\n", num);



        }

        static void disanti()
        {

            //------------第三题----------
            double num = 0;
            for (double i = 1; i <= 98; i++)
            {
                num += i / (i + 1);

            }
            Console.WriteLine("第三题求和:{0}\n\n", num);

        }


        static void disiti()
        {

            //------------第四题----------
            double num = 0;
            for (double i = 0; i < 100; i++)
            {
                num += (i + 1) / (2 * i + 1);

            }
            Console.WriteLine("第三题求和:{0}\n\n", num);
        }



        static void diwuti()
        {
            //------------第五题----------
            Console.WriteLine("请输入4位数");
            string a = Console.ReadLine();
            int c = Convert.ToInt32(a);
            int js = 0;
            for (int i = 2; i < c / 2 + 1; i++)
            {
                if (c % i == 0)
                {
                    js++;
                }
            }
            if (js > 0)
            {
                Console.WriteLine("第五题输入的数:不是素数\n\n");
            }
            else
            {
                Console.WriteLine("第五题输入的数:是素数\n\n");
            }
        }

        static void diliuti()
        {
            //------------第六题----------

            int a = 0;
            int sum = 0;
            for (int i = 0; i < 6; i++)
            {
                a = a * 10 + 4;
                Console.WriteLine(a);
                sum += a;
            }
            Console.WriteLine("第六题求和{0}\n\n", sum);

        }



        static void diqiti()
        {
            //------------第七题----------
            int sum = 0;
            int i = 1;
            int j = 1;
            while (i < 1000)
            {
                sum = 0;
                j = 1;
                while (j < i)
                {
                    if (i % j == 0)
                    {
                        sum += j;
                        // Console.WriteLine(j);
                    }
                    j++;
                }

                if (sum == i)
                {
                    Console.WriteLine("这些完数为{0}", i);
                }
                i++;


            }
        }
        static void dibati()
        {
            //------------第八题----------
            int num = 0;
            int s = 0;
            int i = 1;
            do
            {

                num = 10 * num + i;
                s += num;
                Console.WriteLine(num);
                i++;
            } while (i <= 6);
            Console.WriteLine("求和{0}", s);
        }


    }

你可能感兴趣的:(Unity3D开发-C#语言进阶篇(if与for和while之间应用))