C# 如何从C中的void Main()方法打印int索引的总和#
我试图从控制台上的静态void main获取并打印出一个方法。但是当我试图得到这个方法并打印出来时,我只得到main上最后一个索引的和,但是在这个方法上我可以打印出我需要的值。如果我使用main方法,为什么它不起作用?这就是我得到的:C# 如何从C中的void Main()方法打印int索引的总和#,c#,arrays,console,.net-core-3.0,C#,Arrays,Console,.net Core 3.0,我试图从控制台上的静态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.Lengt
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;
}
您需要返回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);
}
静态列表混合和(int[]v,int[]w)
{
var rx=新列表();
对于(int c=0;c
如果要打印多个值,可以使用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];
}
}
}
static void Main(字符串[]args)
{
int[]v=新的int[]{1,2,3};
int[]w=新的int[]{4,5,6};
foreach(混合总和中的var值(v,w))
{
控制台写入线(值);
}
}
私有静态IEnumerable混合和(int[]v,int[]w)
{
对于(int c=0;c< v长度;C++)
{
对于(int d=0;d
它无法在Main方法中写入正确的值,在我看来,Console方法输出值取决于其执行范围,它只输出方法中的值。
在这种情况下,有两种方法可以实现目标
您可以使用委托:
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;
}
}
使用系统;
公共课程
{
公共静态void Main()
{
int[]v=新的int[]{1,2,3};
int[]w=新的int[]{4,5,6};
Action printInt=i=>Console.WriteLine(i);
混合和(v,w,printInt);
}
静态int MixedSum(int[]v,int[]w,Action printDelegate)
{
int rx=0;
对于(int c=0;c
@Jacman我的错误请尝试此新更新。您还需要返回rx值。我以前也尝试过,编译器说:foreach语句不能对int类型的变量进行操作,因为int不包含公共实例定义GetEnumerator
Hm,所以返回列表,而不是将其转换为数组。预期的输出是什么?5 6 7 8 9您预期的输出是数组,但您正试图将它们存储为int值。我会将它们存储在另一个数组中,并在最后返回这个数组。是的,它会将每个索引的总和打印为(int)00 01 02 10 11 12 20 21 22。垂直地该方法需要为每个索引返回一个int,我必须更改方法签名,但它可以工作。谢谢!什么是操作委托
,它作为集合工作吗?委托只是指向一个函数的指针,可以作为参数传递给另一个函数<代码>动作委托是一种不返回值的委托(与Func
委托相反)。
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;
}
}