C#程序计算算式1+2的1次方+2的2次方+2的3次方+…+2的n次方 的值。 要求:n由键盘输入,且2 ≤ n ≤10。

求具体的解答,想了蛮久都没做出来
2025-05-01 02:14:08
推荐回答(1个)
回答1:



namespace 控制台
{
    class Program
    {
        
        public static void Main(string[] args)  
        {

            Console.WriteLine("请输入一个数字");
            int n = Convert.ToInt32(Console.ReadLine());
            if(checkInput(n))
            {
            Console.WriteLine("结果是" + calculate(n));
            Console.ReadKey();
            }
            else
            {
                Console.WriteLine("您所输入的值不在2 ≤ n ≤10范围内");
                Console.ReadKey();
            }
        }
        
        public static int calculate(int n)
        {
            int result = 0;
            for (int i = 0; i < n; i++) {
                result += 2 ^ n;
            }
            return result;
        }
        
        public static bool checkInput(int n)
        {
            if(n>=2 && n<=10)
            {
                return true;
            }
            return false;
            
        }
    }