使用用户输入数字创建 3x3 矩阵 C#

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/19220942/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-10 14:26:05  来源:igfitidea点击:

Creating a 3x3 matrix with user input numbers C#

c#

提问by user2853957

im trying to create a 3x3 matrix in c# language, i know how to create the matrix but i need help for user input numbers. I hope someone can help me thank you for that.

我试图用 c# 语言创建一个 3x3 矩阵,我知道如何创建矩阵,但我需要用户输入数字的帮助。我希望有人能帮助我谢谢你。

采纳答案by iTURTEV

I will add a while loop and use double.TryParse to validate user's input. Usin BWHazel's code:

我将添加一个 while 循环并使用 double.TryParse 来验证用户的输入。使用 BWHazel 的代码:

const int MATRIX_ROWS = 3;
const int MATRIX_COLUMNS = 3;

double[,] matrix = new double[MATRIX_ROWS, MATRIX_COLUMNS];

for (int i = 0; i < MATRIX_ROWS; i++)
{
    for (int j = 0; j < MATRIX_COLUMNS; j++)
    {
        double input;
        Console.Write("Enter value for ({0},{1}): ", i, j);
        while (!double.TryParse(Console.ReadLine(), out input)
        {
            Console.Write("Enter correct value for ({0},{1}): ", i, j);
        }
        matrix[i,j] = input
    }
}

To get the totals for all rows you can use following snippet:

要获取所有行的总数,您可以使用以下代码段:

for (int i = 0; i < MATRIX_ROWS; i++) 
{
    // The some for each row
    double sum = 0.0;
    for (int j = 0; j < MATRIX_COLUMNS; j++)
    {
        sum += matrix[i,j];
    }
    Console.WriteLine(string.format("The sum for row {0} is: {1}", i, sum));
}

回答by BWHazel

If you are using the command-line, something like this should work:

如果您使用命令行,则应该可以使用以下方法:

const int MATRIX_ROWS = 3;
const int MATRIX_COLUMNS = 3;

double[,] matrix = new double[MATRIX_ROWS, MATRIX_COLUMNS];

for (int i = 0; i < MATRIX_ROWS; i++)
{
    for (int j = 0; j < MATRIX_COLUMNS; j++)
    {
        Console.Write("Enter value for ({0},{1}): ", i, j);
        matrix[i,j] = double.Parse(Console.ReadLine());
    }
}

This assumes you are using doublefor the values. The .Parse()method is available for all .NET numeric types including int.

这假设您正在使用double这些值。该.Parse()方法适用于所有 .NET 数字类型,包括int.

回答by Dileep Kumar M

    private void button1_Click(object sender, EventArgs e)
    {
        txtResult.Text=GenerateMatrix(Int32.Parse(txtRow.Text), Int32.Parse(txtColumn.Text));
    }
    private string GenerateMatrix(int Row,int Column)
    {
        string matrix = string.Empty;
        string Result = string.Empty;
        int nxtline=0;
        for (int i = 0; i < Row; i++)
        {
            for (int j = 0; j < Column; j++)
            {
                if (nxtline==Column)
                {
                    matrix = matrix + Environment.NewLine;
                    nxtline = 0;
                }
                matrix = matrix+"*";
                nxtline = nxtline + 1;
            }
        }

        Result = matrix;
        return Result;
    }