【问题标题】:How to add user input to an array and show it the end如何将用户输入添加到数组并显示结束
【发布时间】:2020-12-19 07:45:14
【问题描述】:

我的代码是一个带有随机数的数学游戏,这是显示问题并要求用户回答的代码的一小部分我如何将每个问题和用户回答存储在一个数组中

  sss = Convert.ToString(answer);
                question = string.Format("{0} {1} {2}", num01, op, num02);
               
                Console.WriteLine("--------------------------------------------------------");
                 Console.WriteLine("What is = " + question+ " Or type QUIT to ignore ");
                useranswer = Convert.ToString(Console.ReadLine());
                

【问题讨论】:

  • 这个问题有很多可能的答案,但您首先应该熟悉使用数组。一个数字数组被声明为int[] numbers = new int[<size>];

标签: c# arrays visual-studio


【解决方案1】:

可以使用二维数组,但必须提前知道要存储多少个问题/答案:

string[,] array = new string[10, 2]; // 10 is how many columns (questions) you want to store, 2 defines that you want to store 2 values (question and answer) in each column
// this is how you add items to your array
array[0, 0] = question; // the first zero defines that you are adding the first item, the second one that it is question
array[0, 1] = answer; //the second one defines that you add answers

您可以像保存问题一样访问问题和答案:array[0, 1] 将返回第一个答案。

如果你不知道你会提前保存多少问题,你应该使用 List 或 Dictionary 而不是数组。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2015-10-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多