在控制台项目中,定义一个静态2维数组{{10.8.20},{20.14.10},{10.20.50},{50.10.25},{4.8.20}},求数组中每行元素的平均值,并分行输出数组元素,并在每行后输出该数组的元素平均值 用Microsoft Visual Studio 这个软件
题主要的代码如下,有帮助麻烦点个采纳【本回答右上角】,谢谢~~
using System;
namespace F3
{
class Program
{
static void Main(string[] args)
{
var items = new double[,]{ { 10, 8, 20 }, { 20, 14, 10 }, { 10, 20, 50 }, { 50, 10, 25 }, { 4, 8, 20} };
var rows = items.GetUpperBound(0)+1;//行数
var cols = items.GetUpperBound(1)+1;//列数
for (var row = 0; row < rows; row++) {
var sum = 0d;
for (var col = 0; col < cols; col++)
{
sum += items[row, col];
Console.Write("{0,-8}", items[row, col] );
}
Console.WriteLine(sum / cols);
}
Console.ReadKey();
}
}
}
using System;
public class Test
{
public static void Main()
{
int[,] a = new int[5, 3] {{10,8,0},{20,14,10},{10,20,50},{50,10,25},{4,8,20}};
double temp = 0,sum=0;
for (int i = 0; i < 5; i++)
{
temp=0;
for (int j = 0; j< 3; j++)
{
temp=temp+a[i,j];
sum+=a[i,j];
}
Console.WriteLine("平均值为{0}", temp/3.0);
}
Console.WriteLine("总体平均值为{0}", sum);
}
}