C#输出在某种程度上是程序名和类名

本文关键字:程序 输出 在某种程度上 | 更新日期: 2023-09-27 17:58:39

我正在尝试执行老师给我的疯狂格式化指令。在仔细阅读了大约一个小时(这是我的第一个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是一个int,包含longestXwhere 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方法。接电话的人让我意识到了这一点。不过,感谢所有给我建议的人。

C#输出在某种程度上是程序名和类名

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

public class Song
{
    public string Title {get; set;}
    public override string ToString()
    {
        return Title;
    }
}