如何从 C# 中的 void Main() 方法打印 int 索引的总和
How to print a Sum of int indexes from the void Main() method in C#
我正在尝试从控制台上的 static void main 获取并打印出一个方法。但是当我试图获取方法并将其打印出来时,我只得到了 main 上最后一个索引的总和,但是在方法上我可以打印我需要的值。如果我在 main 方法上执行它,为什么它不起作用?这是我得到的:
static int MixedSum(int[] v, int[] w)
{
int rx = 0;
for (int c = 0; c < v.Length; c++)
{
for (int d = 0; d < w.Length; d++)
{
rx = v[c] + w[d];
//Console.WriteLine(rx); //Right values gets print out from here.
}
}
return 0;
}
而且,这是主要方法:
static void Main(string[] args)
{
int[] v = new int[] { 1, 2, 3 };
int[] w = new int[] { 4, 5, 6 };
MixedSum(v, w); //I would like to print it out here.
}
您需要 return int 集合而不是一个 int 并在循环中传递结果。
static List<int> MixedSum(int[] v, int[] w)
{
var rx = new List<int>();
for (int c = 0; c < v.Length; c++)
{
for (int d = 0; d < w.Length; d++)
{
rx.Add(v[c] + w[d]);
}
}
return rx;
}
// output
foreach(var num in MixedSum([], [])) {
Console.WriteLine(num);
}
如果要打印多个值,可以使用IEnumerable 和foreach 循环。这是一个例子:
static void Main( string[] args )
{
int[] v = new int[] { 1, 2, 3 };
int[] w = new int[] { 4, 5, 6 };
foreach( var value in MixedSum( v, w ) )
{
Console.WriteLine( value );
}
}
private static IEnumerable<int> MixedSum( int[] v, int[] w )
{
for ( int c = 0; c < v.Length; c++ )
{
for ( int d = 0; d < w.Length; d++ )
{
yield return v[c] + w[d];
}
}
}
不能在Main方法中写入正确的值,在我看来,Console方法输出值取决于它的执行范围,它只输出方法中的值。
以防万一,有两种方法可以实现目标。
在 IEnumerable 列表中使用 yield 关键字。参考这个demo code,直接点击运行看结果
在 MixedSum 方法中定义结果列表,然后 Console.Write 在 Main 方法中定义值。
您可以使用委托:
using System;
public class Program
{
public static void Main()
{
int[] v = new int[] { 1, 2, 3 };
int[] w = new int[] { 4, 5, 6 };
Action<int> printInt = i => Console.WriteLine(i);
MixedSum(v, w, printInt);
}
static int MixedSum(int[] v, int[] w, Action<int> printDelegate)
{
int rx = 0;
for (int c = 0; c < v.Length; c++)
{
for (int d = 0; d < w.Length; d++)
{
rx = v[c] + w[d];
printDelegate(rx);
}
}
return 0;
}
}
我正在尝试从控制台上的 static void main 获取并打印出一个方法。但是当我试图获取方法并将其打印出来时,我只得到了 main 上最后一个索引的总和,但是在方法上我可以打印我需要的值。如果我在 main 方法上执行它,为什么它不起作用?这是我得到的:
static int MixedSum(int[] v, int[] w)
{
int rx = 0;
for (int c = 0; c < v.Length; c++)
{
for (int d = 0; d < w.Length; d++)
{
rx = v[c] + w[d];
//Console.WriteLine(rx); //Right values gets print out from here.
}
}
return 0;
}
而且,这是主要方法:
static void Main(string[] args)
{
int[] v = new int[] { 1, 2, 3 };
int[] w = new int[] { 4, 5, 6 };
MixedSum(v, w); //I would like to print it out here.
}
您需要 return int 集合而不是一个 int 并在循环中传递结果。
static List<int> MixedSum(int[] v, int[] w)
{
var rx = new List<int>();
for (int c = 0; c < v.Length; c++)
{
for (int d = 0; d < w.Length; d++)
{
rx.Add(v[c] + w[d]);
}
}
return rx;
}
// output
foreach(var num in MixedSum([], [])) {
Console.WriteLine(num);
}
如果要打印多个值,可以使用IEnumerable 和foreach 循环。这是一个例子:
static void Main( string[] args )
{
int[] v = new int[] { 1, 2, 3 };
int[] w = new int[] { 4, 5, 6 };
foreach( var value in MixedSum( v, w ) )
{
Console.WriteLine( value );
}
}
private static IEnumerable<int> MixedSum( int[] v, int[] w )
{
for ( int c = 0; c < v.Length; c++ )
{
for ( int d = 0; d < w.Length; d++ )
{
yield return v[c] + w[d];
}
}
}
不能在Main方法中写入正确的值,在我看来,Console方法输出值取决于它的执行范围,它只输出方法中的值。 以防万一,有两种方法可以实现目标。
在 IEnumerable 列表中使用 yield 关键字。参考这个demo code,直接点击运行看结果
在 MixedSum 方法中定义结果列表,然后 Console.Write 在 Main 方法中定义值。
您可以使用委托:
using System;
public class Program
{
public static void Main()
{
int[] v = new int[] { 1, 2, 3 };
int[] w = new int[] { 4, 5, 6 };
Action<int> printInt = i => Console.WriteLine(i);
MixedSum(v, w, printInt);
}
static int MixedSum(int[] v, int[] w, Action<int> printDelegate)
{
int rx = 0;
for (int c = 0; c < v.Length; c++)
{
for (int d = 0; d < w.Length; d++)
{
rx = v[c] + w[d];
printDelegate(rx);
}
}
return 0;
}
}