存储用户输入信息的数组

Array which stores information from user input

C# - 我正在尝试编写一个数组来存储来自用户输入的信息。在我的代码中,我使用了一个名为 friends 的字符串数组,然后我继续使用此方法 。 最后我希望控制台应用程序将存储的信息显示为数组。

enter code here
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using static System.Console;

namespace arraysTut
{
    class arraysTut
    {
        static void Main()

            string[] friends = new string[2];
            for(string i=0;i<friends.length;i++)
            {
                Console.WriteLine("First Friend: ");
                friends[i] = Console.ReadLine();
            
                Console.WriteLine("Second Friend: ");
                friends[i] = Console.ReadLine();
                
                Console.WriteLine(friends);

                Console.ReadLine();
            }
    }
}

除了一些小问题,您几乎已经完成了

var friends = new string[2];

for (var i = 0; i < friends.Length; i++)
{
   Console.Write($"Enter friend ({i + 1}) : ");
   friends[i] = Console.ReadLine();
}

Console.WriteLine();
Console.WriteLine("These are your friends");

foreach (var friend in friends)
   Console.WriteLine(friend);

Console.WriteLine("Game Over!");

输出

Enter friend (1) : bob
Enter friend (2) : john

These are your friends 
bob
john
Game Over!

值得注意的是,List<T> 非常适合这些类型的情况,您不必担心 indexes,只需扩展和 添加列表List.Add(something)