【发布时间】:2016-11-16 18:47:40
【问题描述】:
我是 C# 编程的新手,我在使用二维数组时遇到了很多麻烦。在这个程序中,用户应该为侮辱生成器输入名称、动词和对象,但问题是我必须毫无意义地将它们全部存储在一个二维字符串数组中,并在一维字符串数组中生成侮辱我完全迷路了。我可以将名称、动词和对象存储到单个二维字符串数组中吗?任何帮助将不胜感激。
我的问题是初始化和存储二维字符串数组,然后转换为一维数组。
private static void Generate(int insultnum, int sentnum)
{
int Ncounter = 1;
int Vcounter = 1;
int Ocounter = 1;
string[,] name = new string[sentnum,?];
string[,] verb = new string[sentnum,?];
string[,] insult = new string[sentnum,?];
do
{
Console.Write("Please enter name of #{0}: ", Ncounter);
//length and height 2D array for loop text is just a placeholder from an earlier project
for (int i = 0; i < length; i++)
{
for (int j = 0; j < height; j++)
{
name[Ncounter - 1, ??] = Console.ReadLine();
}
}
//
Ncounter++;
} while (Ncounter < sentnum);
do
{
Console.Write("Please enter name of #{0}: ", Vcounter);
verb[Vcounter-1, ?] = Console.ReadLine();
//2D array loop text
Vcounter++;
} while (Vcounter < sentnum);
do
{
Console.Write("Please enter name of #{0}: ", Ocounter);
insult[Ocounter-1, ?] = Console.ReadLine();
//2D array loop text
Ocounter++;
} while (Ocounter < sentnum);
Ncounter = 0;
Vcounter = 0;
Ocounter = 0;
string[] insults = new string[insultnum];
//insults = name[?,?] + " " + verb[?,?] + " " + object[?,?];
}
示例输出:
输入要产生的侮辱次数:3
输入名称、动词和宾语的数量:3
请输入姓名 #1:Bob
请输入姓名 #2:Rhys
请输入姓名#3:Kaa
请输入动词 #1:舔
请输入动词 #2:touches
请输入动词 #3:味道
请输入对象 #1:污垢
请输入对象 #2:汽车
请输入对象 #3:沥青
已经产生了侮辱Kaa 舔土
鲍勃品尝沥青
鲍勃舔汽车
【问题讨论】:
-
你为什么使用二维数组?
-
因为除了我的老师告诉我之外没有其他原因。
标签: c# arrays string multidimensional-array 2d