c# - 求和、减法和乘法数组

标签 c#

我有以下数组:

int[,] myArray1 = new int[2, 3] { { 1, 2, 3 }, { 4, 6, 8 } };
int[,] myArray2 = new int[2, 3] { { 6, 4, 3 }, { 8, 2, 8 } };

我想知道该怎么做:

  1. 用 myArray1 和 myArray2 的总和创建一个新数组
  2. 通过减去 myArray1 和 myArray2 创建一个新数组
  3. 用 myArray1 和 myArray2 的乘积创建一个新数组

求和结果为:

int[,] myArray3 = new int[2, 3] { { 7, 6, 0 }, { -4, 4, 0 } };

减法的结果是:

int[,] myArray3 = new int[2, 3] { { 5, 2, 6 }, { 12, 8, 16 } };

乘法的结果是:

int[,] myArray3 = new int[2, 3] { { 6, 8, 9 }, { 32, 12, 64 } };

这可以像使用 for 循环打印出数组一样完成吗?我尝试寻找示例,但没有找到可用于我的特定问题的示例。

最佳答案

int[,] a3 = new int[2,3];

for(int i = 0; i < myArray1.GetLength(0); i++)
{
for(int j = 0; j < myArray1.GetLength(1); j++)
{
a3[i,j] = myArray1[i,j] + myArray2[i,j];
a3[i,j] = myArray1[i,j] - myArray2[i,j];
a3[i,j] = myArray1[i,j] * myArray2[i,j];
}
}

显然需要在进行新计算之前存储 a3

关于c# - 求和、减法和乘法数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13928327/

相关文章:

c# - NHibernate:更改父级 - "deleted object would be re-saved by cascade"

c# - 为什么 TPL Dataflow block.LinkTo 不提供任何输出?

c# - 从 int 获取单字节

c# - 在 Visual Studio Code 中恢复依赖项使用了错误的源/提要

c# - 如何在不同的控件中显示列表框选定项的属性

c# - 深度 8 : update user profile

c# - 如何将资源文件中的图标转换为图像以在按钮上使用?

c# - 使用 C# 的谷歌云打印

c# - 以编程方式为 C# 中的文件添加安全权限

c# - 转换格式异常处理