简体   繁体   English

汇总矩形数组C#中的行中的值

[英]Sum up values in Rows in Rectangular Array C#

I'm beginner want to get the Sum each row in 2D Array and print it to the Console and to get the Avg of each column and print it to the Console 我是初学者,想获取2D数组中每一行的总和并将其打印到控制台,并获取每一列的平均数并将其打印到控制台

I wrote my code to get number of rows and cols from the user and use it to create the Array and ask the user to fill each cell in the array 我编写了代码,以从用户那里获取行数和列数,并使用它来创建数组,并要求用户填充数组中的每个单元格

I stuck in sum up each row I don't get any problem in compiling the file I'm getting this error after finishing fill the numbers: 我坚持总结每一行,在编译文件时没有任何问题,在完成填充数字后出现此错误:

at System.Text.StringBuilder.AppendFormat(IFormatProvider provider, String format, Object[] args) at System.String.Format(IFormatProvider provider, String format, Object[] args) at System.IO.TextWriter.WriteLine(String format, Object arg0, Object arg1) at System.IO.TextWriter.SyncTextWriter.WriteLine(String format, Object arg0, Object arg1) at Program.SumRows(Int32[,] arr, Single rows, Int32 cols) at Program.Main() 在System.IO.TextWriter.WriteLine(String,在System.IO.TextWriter.SyncTextWriter.WriteLine(String格式,Object arg0,Object arg1)在Program.SumRows(Int32 [,] arr,单行,Int32 cols)在Program.Main()处的对象arg0,Object arg1)

this is the code: 这是代码:

using System;

struct Program
{
static void Main()
{
    Console.Clear();
    float rows = GetFloatFromUser("Please Enter Number Of Rows: ");
    int cols = GetIntFromUser("Please Enter Number Of Columns: ");
    int[,] recArr = CreatRecArray(rows,cols);
    SumRows(recArr , rows, cols);
    Console.ReadLine();
}

static int GetIntFromUser(string massage)
{
    int result;
    Console.WriteLine(massage);
    result = int.Parse(Console.ReadLine());
    return result;
}
static float GetFloatFromUser(string massage)
{   
    float result;
    Console.WriteLine(massage);
    result = float.Parse(Console.ReadLine());
    return result;
}
static int[,] CreatRecArray(float rows,int cols)
{
int[,] result= new int[(int)rows,cols];
    for (int  i = 0 ; i < rows ; i++)
    {
        for ( int j = 0 ; j < cols ; j++)
        {
            Console.WriteLine("Please Enter Matrix Element {0} , {1} : ",i+1,j+1);
            result[i,j]= int.Parse(Console.ReadLine());
        }
    }
return result;
}
static void SumRows(int[,] arr , float rows,int cols)
{
    for (int i = 1 ; i < rows+1 ; i++)
    {
        int result = 0;
        for ( int j = 0 ; j < cols ; j++)
        {
             result += arr[i,j];

        }
        Console.WriteLine("The Sum of row No {0] = {1}",i,result);
    }
}
}

Try this code: 试试这个代码:

class Program
{
    static void Main()
    {
        Console.Clear();
        float rows = GetFloatInput("Please Enter Number Of Rows: ");
        int cols = GetIntInput("Please Enter Number Of Columns: ");
        int[,] rectangleArray = CreateRectangleArray(rows, cols);
        SumRows(rectangleArray, rows, cols);
        Console.ReadLine();
    }


    static int[,] CreateRectangleArray(float rows, int cols)
    {
        int[,] result = new int[(int)rows, cols];
        for (int i = 0; i < rows; i++)
        {
            for (int j = 0; j < cols; j++)
            {
                Console.WriteLine();
                result[i, j] = GetIntInput($"Please Enter Matrix Element {i+1}, {j+1}");
            }
        }
        return result;
    }

    static void SumRows(int[,] arr, float rows, int cols)
    {
        for (int i = 0; i < rows; i++)
        {
            int result = 0;
            for (int j = 0; j < cols; j++)
            {
                result += arr[i, j];
            }
            Console.WriteLine("The Sum of row No {0} = {1}", i, result);
        }
    }

    private static float GetFloatInput(string message)
    {
        Console.WriteLine(message);
        float result = 0;
        while (!float.TryParse(Console.ReadLine(), out result))
        {
            Console.WriteLine("Failed to parse your input. Make sure it is numeric. Please try again:");
        }
        return result;
    }

    private static int GetIntInput(string message)
    {
        Console.WriteLine(message);
        int result = 0;
        while (!int.TryParse(Console.ReadLine(), out result))
        {
            Console.WriteLine("Failed to parse your input. Make sure it is an integer. Please try again:");
        }
        return result;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM