【问题标题】:Can I ask some questions about C# ? (Array)(Loop) [closed]我可以问一些关于 C# 的问题吗? (数组)(循环)[关闭]
【发布时间】:2019-05-10 05:14:42
【问题描述】:

我正在学习 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 函数,获取一个整数作为输入并计算它的阶乘。它返回阶乘作为函数的结果。在一个简单的程序中使用该函数。在一个简单的程序中使用该函数.)

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);
        }
    }
}

你们能帮帮我吗?

【问题讨论】:

  • 你自己试过了吗?如果您已经写了一些东西,那么提供帮助会更容易。
  • 是的,我试过了.. 但我不明白 MyAvg 和 MyFact 是什么意思
  • 这只是您必须编写的方法的名称。喜欢public int MyFact(int input)
  • 嗨 Seoyoung,欢迎来到 Stack Overflow。不幸的是,SO 想要更具体和实际有问题的问题,因为 C#、.NET 等的基本学习可能比问答网站更适合教学环境。但是,您是否知道 Stack Overflow 附带一整套聊天室?这些比较悠闲,可以接受更多“松散的问题”。您应该在 C# 聊天室中询问,找到 here
  • 我放了一些我自己的代码.. 但我仍然不知道如何制作 Myfact 和 Myavg 的方法(以及“返回”是什么意思?)

标签: c# arrays methods return


【解决方案1】:

您应该更清楚您的问题,并向我们表明您实际上尝试编写一些代码。不管怎样,这里的MyAvgMyFact 只是"MyAvarage""MyFactorial" 的缩写。

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

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

另外,对于函数MyFact,您应该采用一个 int 参数,然后简单地暗示阶乘算法。假设您已将 5 作为参数发送到 MyFact(),那么您应该在 for 循环中递减地乘以数字 5,例如 5* 4*3*2*1,它会给你结果。你可以返回这个结果,或者直接在函数中打印出来。

For loop to calculate factorials

【讨论】:

  • 我放了一些我自己的代码.. 但我仍然不知道如何制作 Myfact 和 Myavg 的方法(以及“返回”是什么意思?)
  • 检查此问题以获取回报; stackoverflow.com/questions/15237649/…
  • 我解决了这个问题!!谢谢!!
  • 如果我的回答对您有帮助,您只需点击检查按钮即可查看。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-05-14
  • 1970-01-01
  • 2020-01-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多