首页 文章

我可以问一些关于C#的问题吗? (数组)(循环)[暂停]

提问于
浏览
-2

我正在学习C#编程 . 不过,我不知道如何解决这些问题 .

喜欢(写一个MyAvg方法得到3个不同的双输入并计算它们的平均值 . 它应该返回平均值作为输出 . 在一个简单的程序中使用该函数)

using System;
/*4. Write a MyAvg method that gets 3 different double inputs and calculates the average of them. It should return the average as the output. Use the function in one simple program*/
namespace ConsoleApp36
    {
        class Program
        {
            static void Main(string[] args)
            {
                double a, b, c;
                double avg = 0;

                Console.Write("Input the first value : ");
                a = Convert.ToDouble(Console.ReadLine());
                Console.Write("Input the second value : ");
                b = Convert.ToDouble(Console.ReadLine());
                Console.Write("Input the third value : ");
                c = Convert.ToDouble(Console.ReadLine());

                avg = (a + b + c) / 3;
                Console.WriteLine("Average of 3 different values is : {0}", avg);
            }

        }
    }

(写一个MyFact函数,它获取一个整数作为输入并计算它的阶乘 . 它返回factorial作为函数的结果 . 在一个简单的程序中使用该函数 . 在一个简单的程序中使用该函数 . )

using System;
/*4. Write a MyAvg method that gets 3 different double inputs and calculates the average of them. It should return the average as the output. Use the function in one simple program*/
namespace ConsoleApp39
{
    class Program
    {
        static void Main(string[] args)
        {
            int i, f = 1, num;
                     
            Console.Write("Input the number : ");
            num = Convert.ToInt32(Console.ReadLine());
            for (i = 1; i <= num; i++)
                f = f * i;

            Console.Write("The Factorial of {0} is: {1}\n", num, f);
        }
    }
}

你们能帮助我吗?

1 回答

  • 0

    你应该更清楚你的问题,并向我们表明你实际上试图编写一些代码 . 无论如何,这里 MyAvgMyFact 只是 "MyAvarage""MyFactorial" 的缩写 .

    正如您应该知道的,数学中的平均数量只是将 n 数字相加,然后将总和除以 n . 所以你应该在你的函数 MyAvg 中实现它 . 这是你应该如何取三个双数的平均值;

    double average = (a + b + c) / 3;
    

    此外,对于函数 MyFact ,您应该使用int参数,然后简单地暗示析因算法 . 让's assume you' ve将 5 作为参数发送到 MyFact() ,然后你应该在for循环中递减数字 5 ,例如 54321 ,它会给你结果 . 您可以返回此结果,或直接在函数中直接打印 .

    For loop to calculate factorials

相关问题