All articles(网络文学目录) All Pictures(图片目录) All Softwares(软件目录)

 
c#中二维数组的使用

Writer: qq Article type: Programming skills(编程技巧) Time: 2014/7/11 9:18:50 Browse times: 311 Comment times: 0

c#中二维数组的使用


Head photo

Go homepage
Upload pictures
Write articles

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();
}
}
}
}





There are 0 records,
Comment:
Must be registered users to comment(必须是注册用户才能发表评论)

Disclaimer Privacy Policy About us Site Map
Copyright ©2011-
uuhomepage.com, Inc. All rights reserved.