2013-02-10 94 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

相关问题