初级到数组 c#

我并不真正理解数组,我需要创建一个“歌曲数组”类型的变量,然后将其初始化为新的数组,以便它可以存储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";  

        }

    }

}


慕容森
浏览 128回答 2
2回答

catspeake

拳头,初始化你的歌曲数组,然后一个简单的for循环就足够了。new Song[ length ]static void Main(string[] args)&nbsp;{&nbsp; &nbsp; Song[] songs = new Song[4];&nbsp; &nbsp; for(int i = 0; i < songs.Length; i++)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; songs[i] = InputSongDetails();&nbsp; &nbsp; }}或者正如评论者所建议的那样,只需使用可变长度的List<Song>。static void Main(string[] args)&nbsp;{&nbsp; &nbsp; List<Song> songs = new List<Song>();&nbsp; &nbsp; for(int i = 0; i < 4; i++)&nbsp;&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; songs.Add(InputSongDetails());&nbsp; &nbsp; }}一旦你掌握了基础知识,你也可以用一点Linq来完成这个(尽管在这种情况下我实际上并不建议这样做):static void Main(string[] args)&nbsp;{&nbsp; &nbsp; var songs = Enumerable.Range(0, 4)&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .Select(i => InputSongDetails())&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; .ToList();}

梦里花落0921

这并不是一个真正的答案,而是一个在控制台应用程序中从用户获取输入的提示,这可能对你有用(好吧,答案在最后一个代码片段中,但p.s.w.g已经很好地涵盖了这一点)。由于交互式控制台会话通常以很多 结束,并且,正如您已经做得很好的那样,在某些情况下对输入进行一些验证,我发现在下面编写以下方法很方便。Console.WriteLine("Ask the user a question"); string input = Console.ReadLine();它们中的每一个都采用 一个 ,这是用户的提示(问题),并返回一个表示其输入的强类型变量。验证(在需要时)全部在方法的循环中完成(如您所做的那样):stringprivate static ConsoleKeyInfo GetKeyFromUser(string prompt){&nbsp; &nbsp; Console.Write(prompt);&nbsp; &nbsp; var key = Console.ReadKey();&nbsp; &nbsp; Console.WriteLine();&nbsp; &nbsp; return key;}private static string GetStringFromUser(string prompt){&nbsp; &nbsp; Console.Write(prompt);&nbsp; &nbsp; return Console.ReadLine();}public static int GetIntFromUser(string prompt = null){&nbsp; &nbsp; int input;&nbsp; &nbsp; int row = Console.CursorTop;&nbsp; &nbsp; int promptLength = prompt?.Length ?? 0;&nbsp; &nbsp; do&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.SetCursorPosition(0, row);&nbsp; &nbsp; &nbsp; &nbsp; Console.Write(prompt + new string(' ', Console.WindowWidth - promptLength - 1));&nbsp; &nbsp; &nbsp; &nbsp; Console.CursorLeft = promptLength;&nbsp; &nbsp; } while (!int.TryParse(Console.ReadLine(), out input));&nbsp; &nbsp; return input;}有了这些方法,获取输入就像:string name = GetStringFromUser("Enter your name: ");int age = GetIntFromUser("Enter your age: ");它使编写方法以从用户那里获取方法变得更加容易:Songprivate static Song GetSongFromUser(){&nbsp; &nbsp; return new Song(&nbsp; &nbsp; &nbsp; &nbsp; GetStringFromUser("Enter song name: "),&nbsp; &nbsp; &nbsp; &nbsp; GetStringFromUser("Enter Artist name: "),&nbsp; &nbsp; &nbsp; &nbsp; GetIntFromUser("Enter number of copies sold: "));}所以现在我们的主要方法看起来像这样(这是你问题的答案):private static void Main(){&nbsp; &nbsp; var songs = new Song[4];&nbsp; &nbsp; for (int i = 0; i < songs.Length; i++)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; songs[i] = GetSongFromUser();&nbsp; &nbsp; }&nbsp; &nbsp; Console.WriteLine("\nYou've entered the following songs: ");&nbsp; &nbsp; foreach (Song song in songs)&nbsp; &nbsp; {&nbsp; &nbsp; &nbsp; &nbsp; Console.WriteLine(song.GetDetails());&nbsp; &nbsp; }&nbsp; &nbsp; GetKeyFromUser("\nDone! Press any key to exit...");}此外,下面是一些改进类的建议。Song
打开App,查看更多内容
随时随地看视频慕课网APP