用户输入到二维数组中 [英] User input into a two dimensional array

查看:37
本文介绍了用户输入到二维数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是C#的新手,我想用简单的代码从用户输入中创建矩阵

I'm completely new to C# and well I would like simple code to create a matrix from user input

E.G.

int [,] matrix1 = new int [2,2]
// now using input i'd like to add integers into the array
matrix1[0,1] = Int32.Parse(Console.ReadLine()); // this is for user input

以此类推.

推荐答案

int[,] A = new int[5, 4];

//read
for (int i = 0; i < 5; i++)
{
    for (int j = 0; j < 4; j++)
    {
        A[i, j] = int.Parse(Console.ReadLine());  
    }
}

//Write
for (int i = 0; i < 5; i++)
{
    Console.WriteLine();

    for (int j = 0; j < 4; j++)
    {
        Console.Write(A[i, j]);
    }
}

这篇关于用户输入到二维数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆