1

我正在尝试做老师给我的疯狂的格式化指令。经过大概一个小时的阅读(这是我的第一个 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));`

longX 是一个 int,包含longestX( where x = title、专辑等) 的字符数。

我想要的输出如下所示:

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 方法。回答的人让我意识到了这一点。不过感谢所有给我建议的人。

4

1 回答 1

1

您必须直接访问一个属性 ( songVariable.Title) 或ToString()在您的歌曲类中覆盖以输出标题。

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

    public override string ToString()
    {
        return Title;
    }
}
于 2013-02-10T17:51:48.233 回答