c# 长度相同的int数组相加怎么计算
你可以使用一个循环来逐个元素相加两个数组的对应元素,然后将结果存储在一个新的数组中。以下是一个示例代码:
int[] array1 = { 1, 2, 3 };
int[] array2 = { 4, 5, 6 };
int[] result = new int[array1.Length];
for (int i = 0; i < array1.Length; i++)
{
result[i] = array1[i] + array2[i];
}
// 输出结果
foreach (int num in result)
{
Console.WriteLine(num);
}
上述代码将输出:
5
7
9
原文地址: https://www.cveoy.top/t/topic/inpd 著作权归作者所有。请勿转载和采集!