C# 如何将数组传递给使用输出参数的方法
我有一个有两个输出参数的方法。该方法应获取一个数组,并返回数组中值的总和和平均值。还有另一种方法可以从用户输入创建数组。需要从main方法初始化数组。我真的被这个难住了。我希望你们能帮忙。我在下面包含了我的代码C# 如何将数组传递给使用输出参数的方法,c#,.net,C#,.net,我有一个有两个输出参数的方法。该方法应获取一个数组,并返回数组中值的总和和平均值。还有另一种方法可以从用户输入创建数组。需要从main方法初始化数组。我真的被这个难住了。我希望你们能帮忙。我在下面包含了我的代码 // Create a console-based application whose Main() method declares an array of eight integers. // // Call a method to interactivelyfill the arra
// Create a console-based application whose Main() method declares an array of eight integers.
//
// Call a method to interactivelyfill the array with any number of values up to eight.
//
// Call a second method that accepts out parameters for the arithmetic average and the sum of the values in the array.
//
// Display the array values, the number of entered elements, and their average and sum in the Main() method.
using System;
namespace ArrayManagment
{
class Program
{
static void arrayMath(out int sum, out int avg)
{
sum = myArray.Sum();
avg = myArray.Average();
}
static void displayArray(int[] myArray)
{
Console.Write("Your numbers are: ");
for (int i = 0; i < 8; i++)
Console.Write(myArray[i] + " ");
Console.WriteLine();
}
static int[] fillArray()
{
int[] myArray;
myArray = new int[8];
int count = 0;
do
{
Console.Write("Please enter a number to add to the array or \"x\" to stop: ");
string consoleInput = Console.ReadLine();
if (consoleInput == "x")
{
return myArray;
}
else
{
myArray[count] = Convert.ToInt32(consoleInput);
++count;
}
} while (count < 8);
return myArray;
}
static void Main(string[] args)
{
int[] myArray;
myArray = new int[8];
myArray = fillArray();
int sum, avg;
arrayMath(out sum, out avg);
displayArray(myArray);
}
}
}
//创建一个基于控制台的应用程序,其Main()方法声明一个由八个整数组成的数组。
//
//调用一个方法以交互方式向数组填充最多8个值。
//
//调用第二个方法,该方法接受算术平均值和数组中值之和的out参数。
//
//在Main()方法中显示数组值、输入的元素数及其平均值和总和。
使用制度;
命名空间数组管理
{
班级计划
{
静态void arrayMath(out int sum,out int avg)
{
sum=myArray.sum();
avg=myArray.Average();
}
静态void displayArray(int[]myArray)
{
控制台。写下(“你的号码是:”);
对于(int i=0;i<8;i++)
Write(myArray[i]+“”);
Console.WriteLine();
}
静态int[]fillArray()
{
int[]myArray;
myArray=新整数[8];
整数计数=0;
做
{
Console.Write(“请输入一个要添加到数组中的数字或\“x\”停止:”);
字符串consoleInput=Console.ReadLine();
如果(控制台输入=“x”)
{
返回myArray;
}
其他的
{
myArray[count]=转换为32(控制台输入);
++计数;
}
}而(计数<8);
返回myArray;
}
静态void Main(字符串[]参数)
{
int[]myArray;
myArray=新整数[8];
myArray=fillArray();
平均整数和;
arrayMath(求和,求平均值);
显示阵列(myArray);
}
}
}
只需将其作为参数放在输出参数之前:
static void arrayMath(int[] myArray, out int sum, out int avg)
(不需要在输出参数之前,但它只是有意义的。)
然后将数组发送到Main
方法中的方法:
arrayMath(myArray, out sum, out avg);
只需将其作为参数放在输出参数之前:
static void arrayMath(int[] myArray, out int sum, out int avg)
(不需要在输出参数之前,但它只是有意义的。)
然后将数组发送到Main
方法中的方法:
arrayMath(myArray, out sum, out avg);
不要求任何人做这项工作。我请求帮助理解如何将数组传递给方法。所有的代码都是我的,我只是不知道如何去做我想做的事情。不要求任何人去做这项工作。我请求帮助理解如何将数组传递给方法。所有的代码都是我的,我只是不知道如何去做我想做的。Guffa之所以说把数组放在
out
参数之前,是因为通常认为把out
参数放在末尾是最好的做法。好吧,我考虑过这样做。只是觉得我不能同时做这两件事。谢谢你的帮助,Guffa!Guffa之所以说将数组放在out
参数之前,是因为通常认为将out
参数放在末尾是最佳做法。好吧,我考虑过这样做。只是觉得我不能同时做这两件事。谢谢你的帮助,Guffa!