用C#打印2D数组或矩阵
首先,设置一个二维数组。
int[,] arr = new int[10, 10];
现在,向用户获取元素−
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
arr[i, j] = Convert.ToInt16(Console.ReadLine());
}
}让我们看一个完整的示例来显示该矩阵。
示例
using System;
using System.Linq;
class Demo {
static void Main() {
int m, n, i, j;
// rows and columns of the matrix+
m = 2;
n = 2;
int[,] arr = new int[10, 10];
Console.Write("Enter elements of the Matrix: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
arr[i, j] = Convert.ToInt16(Console.ReadLine());
}
}
Console.WriteLine("Printing Matrix: ");
for (i = 0; i < m; i++) {
for (j = 0; j < n; j++) {
Console.Write(arr[i, j] + "\t");
}
Console.WriteLine();
}
Console.ReadLine();
}
}输出
以下是输出。
Enter elements of the Matrix: 5 10 12 15 Printing Matrix: 510 1215
广告
数据结构
网络
关系型数据库管理系统
操作系统
Java
iOS
HTML
CSS
Android
Python
C编程
C++
C#
MongoDB
MySQL
JavaScript
PHP