C#二维数组

扫码查看

数组格式

             一维数组:
Console.WriteLine("输入班级人数");
int renshu = int.Parse(Console.ReadLine());
int[] chengji = new int[renshu]; 。。。。。。。。。。 初始化
for (int i = ; i < renshu ;i++ )
{
Console.WriteLine("输入第{0}个人的成绩;",i+);
chengji[i] = int.Parse(Console.ReadLine());。。。。。。。赋值
}
Console.WriteLine("打印成绩");
Console.ReadLine();
Console.Clear();
for (int i = ; i < renshu;i++ )
{
Console.Write(chengji[i]+"\t");。。。。。。。。。。。。。打印方法一
} //foreach(int aa in chengji )
//{
// Console.WriteLine(aa); 。。。。。。。。。打印方法2
//}
Console.ReadLine();

数组格式1举例

二维数组

 int[,] array = new int[, ]{

 {,,,},

  {,,,},

 {,,,} };

 3表示,有三个一维数组

 4表示,每一个一维数组中有4个元素

二维数组

split()    以***进行分割
分割开的内容需要放置在string类型的数组中,不需要给数组定义长度

             Console.Write("请输入姓名-年龄-工作单位:");
//"张三-33-汉企"
string s = Console.ReadLine();
string[] array = s.Split('-');
foreach (string aa in array)
{
Console.WriteLine(aa);
}

split例题

打印图形

             string[,] wang = new string[,] {
{" ","■","■","■","■","■",""},
{" "," "," ","■","","",""},
{" "," "," ","■","","",""},
{" "," ","■","■","■","",""},
{" "," "," ","■","","",""},
{" "," "," ","■","","",""},
{"■","■","■","■","■","■","■"}
};
for (int i = ; i < ;i++ )
{
for (int j = ; j < ;j++ )
{
Console.Write(wang[i,j]);
}
Console.WriteLine();
}

打印一个王字

一维数组例题

             输入班级人数,输入每个人的人名及分数
排序,知道最高分是多少,是谁考得
Console.Write("请输入班级人数:");
int a = int.Parse(Console.ReadLine());
string [] name = new string[a];
double [] score =new double[a]; for (int i = ; i < a;i++ )
{
Console.Write("请输入第{0}个姓名:",(i+));
name[i] = Console.ReadLine();
Console.Write("请输入第{0}个人的分数:",(i+));
score[i] = double.Parse(Console.ReadLine());
} for (int i = ; i < a - ;i++ )
{
for (int j = i + ; j < a;j++ )
{
if (score[i] < score[j])
{
double zhong = score[i];
score[i] = score[j];
score[j] = zhong;
string zh = name[i];
name[i] = name[j];
name[j] = zh;
}
}
}
Console.WriteLine("最高分的学生是:{0},他的分数是:{1}",name[],score[]);

打印最高分方法一

//输入班级人数,输入每个人的人名及分数
//排序,知道最高分是多少,是谁考得
//要求使用一个一维数组
Console.Write("请输入班级人数:");
int renshu = int.Parse(Console.ReadLine());
string[] str = new string[renshu*];
int zhi = ;
for (int i = ; i < renshu * ; i += )
{
zhi++;
Console.Write("请输入第{0}个人的姓名:",zhi);
str[i] = Console.ReadLine();
Console.Write("请输入第{0}个人的成绩:", zhi);
str[i+] = Console.ReadLine(); }
for (int i = ; i < renshu * -; i += )
{
for (int j = i + ; j < renshu * ; j += )
{
if (double.Parse(str[i + ]) < double.Parse(str[j]))
{
string zhong = str[i];
str[i] = str[j - ];
str[j - ] = zhong;
zhong = str[i + ];
str[i + ] = str[j];
str[j] = zhong;
}
}
}
Console.WriteLine("最高分是{0},是{1}考得", str[], str[]); Console.ReadLine();

打印最高分

二维数组替换

             Console.WriteLine("输入要替换的字");
string c = Console.ReadLine();
Console.WriteLine("输入要替换wei的字");
string d = Console.ReadLine(); string[,] array = new string[, ] {
{ "春","眠","不","觉","晓",","},
{ "处","处","闻","啼","鸟",","},
{ "夜","来","风","雨","声",","},
{ "花","落","知","多","少","。"} };
for (int i = ; i < ; i++)
{
for (int j = ; j < ; j++)
{
if (array[i, j] == c)
{
array[i, j] = d;
}
} }
foreach (string aa in array)
{
Console.Write(aa);
} Console.ReadLine();

替换诗句中的词

04-25 19:39
查看更多