亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

初級到數組 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條經驗 獲得超6個贊

這并不是一個真正的答案,而是一個在控制臺應用程序中從用戶獲取輸入的提示,這可能對你有用(好吧,答案在最后一個代碼片段中,但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 關注
  • 114 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號