怎么用C#在控制台中声明一个数组?

要求:
元素个数 元素由控制台输入

using System;
using System.Collections.Generic;
using System.Text;

class Arr
{
static void Main()
{
int nLength; //声明数组长度变量
int[] arr; //声明数组

Console.WriteLine("请输入数组大小");
nLength = Int32.Parse(Console.ReadLine());
arr = new int[nLength];

Console.WriteLine("请输入数组元素");
for (int i = 0; i < nLength; i++) //循环读入数组元素值
{
arr[i] = Int32.Parse(Console.ReadLine());
}

Console.WriteLine("打印数组元素");
for (int i = 0; i < nLength; i++) //循环打印数组元素值
{
Console.WriteLine("arr[{0}]={1}", i, arr[i]);
}
}
}
温馨提示:内容为网友见解,仅供参考
第1个回答  2013-10-26
我爱bobo一辈子!!!爱你到死!!!
第2个回答  2013-10-26
string[][] fn=new string[12][];
fn[0]=new string[]{...}
fn[1]=new string[]{...}
...
fn[11]=new string[]{...}//这些写了吗

string[][] fn=new string[12][];
这一句只声明了包含12个string类型的一维数组的引用变量的一维数组。c#中数组是对象,必须实例化。
fn[0]=new string[]{...}
fn[1]=new string[]{...}
...
fn[11]=new string[]{...}
这些就是实例化每个string型一维数组。初始化可以写在大括号内。
二维数组是这样的
string[,] fn=new string[m,n]//m,n为常量表达式
第3个回答  2013-10-26
编程
相似回答