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

Posted

技术标签:

【中文标题】我可以问一些关于 C# 的问题吗? (数组)(循环)[关闭]【英文标题】:Can I ask some questions about C# ? (Array)(Loop) [closed] 【发布时间】: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 的方法(以及“返回”是什么意思?) 【参考方案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 的方法(以及“返回”是什么意思?) 检查此问题以获取回报; ***.com/questions/15237649/… 我解决了这个问题!!谢谢!! 如果我的回答对您有帮助,您只需点击检查按钮即可查看。

以上是关于我可以问一些关于 C# 的问题吗? (数组)(循环)[关闭]的主要内容,如果未能解决你的问题,请参考以下文章

我可以获得一些关于如何通过在 C# 中使用 webhook API 从 Forge 获取文件翻译进度百分比的帮助/示例吗?

C# 循环遍历给定标签的数组

我错过了一些关于 LINQ 的东西吗?

在 C++ 中记录经过时间的正确方法

C# 填充数组

可以用 C# 构建一个 64 位 ActiveX 控件供企业应用程序使用吗