编程实践65

编程实践6-5

课程元素类型 任务

定义函数,计算a+aa+aaa+...+a…a,共n项,其中a、n由键盘输入,a是[1,9]间的数字。

using System;

namespace sj_6_5
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, n;

            Console.Write("请输入A的值:\n");
            a = Convert.ToInt32(Console.ReadLine());
            Console.Write("请输入N的值:\n");
            n = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine(js(a, n));

            Console.ReadKey();
        }
        public static int js(int x, int y)
        {
            int c;
            string d;
            c = x;
            d = "11";
            for (int i = 0; i < y - 1; i++)
            {
                c = c + x * Convert.ToInt32(d);
                d = d + "1";
            }
            return c;
        }
    }
}

.版本2

using System;

namespace sj_6_5
{
    class Program
    {
        static void Main(string[] args)
        {
            int a, n;

            Console.Write("请输入A的值:\n");
            a = Convert.ToInt32(Console.ReadLine());
            Console.Write("请输入N的值:\n");
            n = Convert.ToInt32(Console.ReadLine());

            Console.WriteLine(js(a, n));

            Console.ReadKey();
        }
        public static int js(int x, int y)
        {
            int a = 0,c = 0;
            
            for (int i = 0; i < y; i++)
            {
                a = a * 10 + x;
                c = c + a;
            }
            return c;
        }
    }
}
原文地址:https://www.cnblogs.com/Wzqa/p/2812344.html