为了账号安全,请及时绑定邮箱和手机立即绑定

初级到数组 c#

初级到数组 c#

C#
慕容森 2022-08-20 16:15:05
我并不真正理解数组,我需要创建一个“歌曲数组”类型的变量,然后将其初始化为新的数组,以便它可以存储4个对歌曲的引用。然后,我将如何创建一个循环,该循环将运行足够的时间以在调用该方法时填充数组并将返回值存储在该方法中?InputSOngDetails()namespace Songs{    class Program    {        static void Main(string[] args) {            InputSongDetails();        }        static Song InputSongDetails()        {            Console.WriteLine("What is the name of your song");            string name = Console.ReadLine();            Console.WriteLine("What is the artists name");            string artist = Console.ReadLine();            int records;            Console.WriteLine("How many records did it sell");            while (!int.TryParse(Console.ReadLine(), out records) || records < 0)            {                Console.WriteLine("That is not valid please enter a number");            }            return new Song(name, artist, records);        }    }}如果需要,这是我的歌曲课程namespace Songs{    class Song    {        string name;        string artist;        int copiesSold;        public Song(string name, string artist, int copiesSold)        {            this.name = name;            this.artist = artist;            this.copiesSold = copiesSold;        }        public Song()        {        }        public string GetArtist()        {            return artist;        }        public string GetDetails()        {            return $"Name: {name} Artist: {artist} Copies Sold: {copiesSold},";        }        public string GetCertification()        {            if (copiesSold<200000)            {                return null;            }            if (copiesSold<400000)            {                return "Silver";            }            if (copiesSold<600000)            {                return "gold";            }            return "Platinum";          }    }}
查看完整描述

2 回答

?
catspeake

TA贡献1111条经验 获得超0个赞

拳头,初始化你的歌曲数组,然后一个简单的for循环就足够了。new Song[ length ]


static void Main(string[] args) 

{

    Song[] songs = new Song[4];

    for(int i = 0; i < songs.Length; i++) 

    {

        songs[i] = InputSongDetails();

    }

}

或者正如评论者所建议的那样,只需使用可变长度的List<Song>。


static void Main(string[] args) 

{

    List<Song> songs = new List<Song>();

    for(int i = 0; i < 4; i++) 

    {

        songs.Add(InputSongDetails());

    }

}

一旦你掌握了基础知识,你也可以用一点Linq来完成这个(尽管在这种情况下我实际上并不建议这样做):


static void Main(string[] args) 

{

    var songs = Enumerable.Range(0, 4)

                          .Select(i => InputSongDetails())

                          .ToList();

}


查看完整回答
反对 回复 2022-08-20
?
梦里花落0921

TA贡献1772条经验 获得超5个赞

这并不是一个真正的答案,而是一个在控制台应用程序中从用户获取输入的提示,这可能对你有用(好吧,答案在最后一个代码片段中,但p.s.w.g已经很好地涵盖了这一点)。


由于交互式控制台会话通常以很多 结束,并且,正如您已经做得很好的那样,在某些情况下对输入进行一些验证,我发现在下面编写以下方法很方便。Console.WriteLine("Ask the user a question"); string input = Console.ReadLine();


它们中的每一个都采用 一个 ,这是用户的提示(问题),并返回一个表示其输入的强类型变量。验证(在需要时)全部在方法的循环中完成(如您所做的那样):string


private static ConsoleKeyInfo GetKeyFromUser(string prompt)

{

    Console.Write(prompt);

    var key = Console.ReadKey();

    Console.WriteLine();

    return key;

}


private static string GetStringFromUser(string prompt)

{

    Console.Write(prompt);

    return Console.ReadLine();

}


public static int GetIntFromUser(string prompt = null)

{

    int input;

    int row = Console.CursorTop;

    int promptLength = prompt?.Length ?? 0;


    do

    {

        Console.SetCursorPosition(0, row);

        Console.Write(prompt + new string(' ', Console.WindowWidth - promptLength - 1));

        Console.CursorLeft = promptLength;

    } while (!int.TryParse(Console.ReadLine(), out input));


    return input;

}

有了这些方法,获取输入就像:


string name = GetStringFromUser("Enter your name: ");

int age = GetIntFromUser("Enter your age: ");

它使编写方法以从用户那里获取方法变得更加容易:Song


private static Song GetSongFromUser()

{

    return new Song(

        GetStringFromUser("Enter song name: "),

        GetStringFromUser("Enter Artist name: "),

        GetIntFromUser("Enter number of copies sold: "));

}

所以现在我们的主要方法看起来像这样(这是你问题的答案):


private static void Main()

{

    var songs = new Song[4];


    for (int i = 0; i < songs.Length; i++)

    {

        songs[i] = GetSongFromUser();

    }


    Console.WriteLine("\nYou've entered the following songs: ");


    foreach (Song song in songs)

    {

        Console.WriteLine(song.GetDetails());

    }


    GetKeyFromUser("\nDone! Press any key to exit...");

}

此外,下面是一些改进类的建议。Song


查看完整回答
反对 回复 2022-08-20
  • 2 回答
  • 0 关注
  • 76 浏览

添加回答

举报

0/150
提交
取消
意见反馈 帮助中心 APP下载
官方微信