我编写了这段代码来从用户那里获取数组值并显示它们。
namespace Program_2
{
class Program
{
static void Main(string[] args)
{
int[,] nums = new int[6,2];
for (int x = 0; x < 5; x++)
{
for (int y = 0; y < 2; y++)
{
Console.WriteLine("Enter Numbers");
nums[x, y] = int.Parse(Console.ReadLine());
}
}
for(int j=0; j < 5; j++)
{
for(int k = 0; k < 2; k++)
{
Console.Write(nums[j, k]+" ");
}
Console.WriteLine("");
}
Console.ReadLine();
}
}
}
但我想知道如何进行用户输入的数组的乘法和加法。
喜欢这个"See this image"
和
int total = 0;
// Iterate first dimension of the array
for (int i = 0; i < nums.GetLength(0); i++)
{
// Iterate second dimension
for (int j = 0; j < nums.GetLength(1); j++)
{
total += array[i, j];
}
}
Console.WriteLine(total);
MULTIFICATION
int total = 0;
// Iterate first dimension of the array
for (int i = 0; i < nums.GetLength(0); i++)
{
// Iterate second dimension
for (int j = 0; j < nums.GetLength(1); j++)
{
total *= array[i, j];
}
}
Console.WriteLine(total);
两个一个
int sum= 0;
int mul= 0;
// Iterate first dimension of the array
for (int i = 0; i < nums.GetLength(0); i++)
{
// Iterate second dimension
for (int j = 0; j < nums.GetLength(1); j++)
{
sum += array[i, j];
mul *= array[i, j];
}
}
Console.WriteLine("SUM = " + sum);
Console.WriteLine("MUL = " + mul);