Go homepage(回首页)
Upload pictures (上传图片)
Write articles (发文字帖)

The author:(作者)qq
published in(发表于) 2014/7/11 9:18:50
c#中二维数组的使用

c#中二维数组的使用

c#中二维数组的使用

需要存储表格式的数据时,可以使用二维数组,图1所示为包括4行3列的二维数组的存储结构。



图1 二维数组的存储结构

* 示例

二维数组的使用

示例中,将用户输入行值和列值作为二维数组的长度,并将数组行索引和列索引相同的数组元素输出为“*”,其他元素输出为“@”。程序代码如下:

Console.Write("请输入定义数组的行数:");

int row = Convert.ToInt32 (Console.ReadLine());

Console.Write("请输入定义数组的列数:");

int col = Convert.ToInt32 (Console.ReadLine());

//本教程来自网站源代码http://www.isstudy.com

int[,] arr2 = new int[row, col];

Console.WriteLine("结果:");

for (int i = 0; i < row; i++)

{

for (int j = 0; j < col; j++)

{

if (i == j)

{

Console.Write("*");

}

else

{

Console.Write("@");

}

}

Console.WriteLine();

}

键运行程序,运行结果如图2所示。



图2 二维数组示例运行结果图

完整程序代码如下:

★★★★★主程序文件完整程序代码★★★★★

using System;

using System.Collections.Generic;

using System.Text;

namespace _2

{

class Program

{

static void Main(string[] args)

{

Console.Write("请输入定义数组的行数:");

int row = Convert.ToInt32 (Console.ReadLine());

Console.Write("请输入定义数组的列数:");

//本教程来自网站源代码http://www.isstudy.com

int col = Convert.ToInt32 (Console.ReadLine());

int[,] arr2 = new int[row, col];

Console.WriteLine("结果:");

for (int i = 0; i < row; i++)

{

for (int j = 0; j < col; j++)

{

if (i == j)

{

Console.Write("*");

}

else

{

Console.Write("@");

}

}

Console.WriteLine();

}

}

}

}




If you have any requirements, please contact webmaster。(如果有什么要求,请联系站长)





QQ:154298438
QQ:417480759