2013-02-10 88 views
1

我想要做老師給我的瘋狂的格式化指令。仔細閱讀大概一個小時後(這是我的第一個C#程序),我想出了這行代碼。C#輸出以某種方式程序名稱和類名

`Console.WriteLine(String.Format("{0," + -longestTitle + "} | {1," + -longestAlbumTitle + "} | {2," + -longestArtist + "} | {3:0.00, 8} | {4," + -longestYearAndRating + "} |", songArray[arraySearcher].title, songArray[arraySearcher].albumTitle, songArray[arraySearcher].artist, songArray[arraySearcher].length, songArray[arraySearcher].yearAndRating));` 

longestX是包含longestX的字符數(where x = title,專輯等)一個int。

我想輸出看起來是這樣的:

Stuff | morestuff | extrastuff | 5.92 | 1992:R | 
Stuf | est  | sfafe  | 232.44 | 2001:PG | 
S uf | e   | sfe  | .44 | 2001:G | 

(其中所有的填充,動態地確定基於用戶或文件最長的標題輸入)。

我得到的輸出是這樣的:

Program_Example.ClassName 
Program_Example.ClassName 

(或具體爲Tyler_Music_Go.Song)

我已經印刷songArray[arraySearcher] .title僞在這同樣的方法,並能正常工作。

有人能幫我嗎?

全部相關代碼:

class Song { 
    public string title, albumTitle, yearAndRating, artist; 
    public float length; 

    public Song(string titl, string albumTitl, string art, float leng, string yrNRating) 
    { 
     title = titl; 
     albumTitle = albumTitl; 
     yearAndRating = yrNRating; 
     length = leng; 
     artist = art; 
    } 
} 

//This class contains a Song array (with all Songs contained within), an array index, a search index, and ints to determine the longest of each category. 
class SongList 
{ 
    Song[] songArray; 
    private int arrayKeeper, longestTitle, longestArtist, longestAlbumTitle, longestYearAndRating, checker; 
    int arraySearcher = 0; 

    public SongList() 
    { 
     songArray = new Song[10000]; 
     arrayKeeper = 0; 
     longestTitle = 0; 
     longestArtist = 0; 
     longestAlbumTitle = 0; 
     longestYearAndRating = 0; 
    } 

    public void AddSong(string title, string albumTitle, string artist, float length, string yearAndRating) 
    { 
     songArray[arrayKeeper] = new Song(title, albumTitle, artist, length, yearAndRating); 
     arrayKeeper++; 
     checker = 0; 

     //This section of code is responsible for formatting the output. Since the longest values are already known, the list can be displayed quickly. 
     //Once a song is deleted, however, previously calculated longest lengths still stand. 
     foreach (char check in title) 
     { 
      checker++; 
     } 
     if (checker > longestTitle) 
     { 
      longestTitle = checker; 
     } 

     foreach (char check in albumTitle) 
     { 
      checker++; 
     } 
     if (checker > longestAlbumTitle) 
     { 
      longestAlbumTitle = checker; 
     } 

     foreach (char check in artist) 
     { 
      checker++; 
     } 
     if (checker > longestArtist) 
     { 
      longestArtist = checker; 
     } 

     foreach (char check in yearAndRating) 
     { 
      checker++; 
     } 
     if (checker > longestYearAndRating) 
     { 
      longestYearAndRating = checker; 
     } 

    } 

    //public bool RemoveSong(string title) 
    // { 
    //} 

    public void DisplayData() 
    { 
     Console.WriteLine("|   Title   |   Album Title   |   Artist   |   Length   |   Year and Rating   |"); 
     for (arraySearcher = 0; arraySearcher < arrayKeeper; arraySearcher++) 
     { 
      //This line for testing purposes. (works) 
      Console.WriteLine(songArray[arraySearcher].title); 
      Console.WriteLine(songArray[arraySearcher].ToString()); 
     } 
    } 

    public override string ToString() 
    { 
     //This line for testing purposes. (works) 
     Console.WriteLine(songArray[arraySearcher].title); 
     return String.Format("{0," + -longestTitle + "} | {1," + -longestAlbumTitle + "} | {2," + -longestArtist + "} | {3:0.00, 8} | {4," + -longestYearAndRating + "} |", songArray[arraySearcher].title, songArray[arraySearcher].albumTitle, songArray[arraySearcher].artist, songArray[arraySearcher].length, songArray[arraySearcher].yearAndRating); 
    } 
} 

`

編輯:

好了,現在我覺得自己愚蠢的所有莊園。我正在覆蓋SongList的tostring()方法,然後調用Song的tostring方法。回答的那個人讓我意識到了這一點。不過,感謝所有給我建議的人。

+0

如果你想讓你的Song類輸出別的東西然後輸出它自己的類名,那麼你將不得不重寫ToString()方法。 – 2013-02-10 17:48:27

+0

我重寫了tostring方法。也許我應該只發布整個事情。 – 2013-02-10 17:49:30

+0

請把你的輸出功能分成多行。爲每列引入一些臨時變量,比如'formattedTitle',然後結合它們。 – CodesInChaos 2013-02-10 17:50:57

回答

1

您必須直接訪問屬性(songVariable.Title)或覆蓋歌曲類中的ToString()以使輸出標題。

public class Song 
{ 
    public string Title {get; set;} 

    public override string ToString() 
    { 
     return Title; 
    } 
} 
+0

如果是這樣的話,爲什麼Console.WriteLine(songArray [arraySearcher] .title); 就行了嗎?是否因String.Format而不同? – 2013-02-10 17:59:32

相關問題