I am trying to make a potential two player program where one user is prompted to enter a question and then prompted to enter the answer to that question both of which will be stored in a two dimensional array. The first player will be able to enter up to 10 questions. After both the question and answer to that question are stored, I would like to then be able to have the second player prompted to answer the questions the first player asked.
我正在尝试创建一个潜在的两个播放器程序,其中一个用户被提示输入问题,然后提示输入该问题的答案,这两个问题都将存储在二维数组中。第一位玩家最多可以输入10个问题。在存储了该问题的问题和答案之后,我希望能够让第二个玩家回答第一个玩家提出的问题。
Right now I'm stuck at a pretty basic part which is storing the questions and answers in the array.
现在我陷入了一个非常基本的部分,它将问题和答案存储在数组中。
Here is the code I have so far my first class:
这是我到目前为止我的第一堂课的代码:
class MakeOwnQuestion
{
string question;
string answer;
string[,] makequestion = new string[10, 2];
public void MakeQuestion(string question, string answer, int index)
{
if (index < makequestion.Length)
{
makequestion[index, 0] = question;
makequestion[index, 1] = answer;
}
}
My second class:
我的第二堂课:
class MakeOwnQuestionUI
{
MakeOwnQuestion newquestion;
public void MainMethod()
{
PopulateArray();
}
void PopulateArray()
{
string question;
string answer;
Console.WriteLine("Enter Your Question: ");
question = Console.ReadLine();
Console.WriteLine("Enter Your Answer: ");
answer = Console.ReadLine();
newquestion.MakeQuestion(question, answer, 0);
Console.WriteLine("Enter Your Question: ");
question = Console.ReadLine();
Console.WriteLine("Enter Your Answer: ");
answer = Console.ReadLine();
newquestion.MakeQuestion(question, answer, 1);
}
}
I keep getting the same error message after the user enters their first answer "Object reference not set to an instance of an object"
在用户输入第一个答案“对象引用未设置为对象的实例”后,我不断收到相同的错误消息
1 个解决方案
#1
7
You need to initialize your newquestion
instance:
您需要初始化newquestion实例:
MakeOwnQuestion newquestion = new MakeOwnQuestion();
I'd also recommend you use GetLength
rather than Length
for a multidimensional array:
我还建议你使用GetLength而不是Length作为多维数组:
if (index < makequestion.GetLength(0))
{
...
}
Or better yet, just a List<T>
of some type, e.g. Tuple<string, string>
:
或者更好的是,只是某种类型的List
class MakeOwnQuestion
{
List<Tuple<string, string>> makequestion = new List<Tuple<string, string>>();
public void MakeQuestion(string question, string answer, int index)
{
makequestion.Add(Tuple.Create(question, answer));
}
}
#1
7
You need to initialize your newquestion
instance:
您需要初始化newquestion实例:
MakeOwnQuestion newquestion = new MakeOwnQuestion();
I'd also recommend you use GetLength
rather than Length
for a multidimensional array:
我还建议你使用GetLength而不是Length作为多维数组:
if (index < makequestion.GetLength(0))
{
...
}
Or better yet, just a List<T>
of some type, e.g. Tuple<string, string>
:
或者更好的是,只是某种类型的List
class MakeOwnQuestion
{
List<Tuple<string, string>> makequestion = new List<Tuple<string, string>>();
public void MakeQuestion(string question, string answer, int index)
{
makequestion.Add(Tuple.Create(question, answer));
}
}