0

你好我有一个小问题。

我使用的是时间结构,如果我将秒数设置为 100,它必须给我一个输出(在控制台中):1 分 40 秒。

struct Time
    {
        Time Timer = new Time(100);
        public Timer(int seconds);
        public Timer(int minutes, int seconds);
        public Timer(int hours, int minutes, int seconds);
    }

我有这个,但它给了我一个错误:“方法必须有一个返回类型”“AudioDevices.Tracks.Track.Time 不包含带有 1 个参数的构造函数”

完整代码:

public class Track
{
    int id;
    string name;
    string artist;
    string albumSource;

    enum Category
    {
        Ambient, Blues, Country, Disco, Electro, Hardcore, HardRock,
        HeavyMetal, Hiphop, Jazz, Jumpstyle, Klassiek, Latin, Other,
        Pop, Punk, Reggae, Rock, Soul, Trance, Techno,
    }

    struct Time
    {
        Time Timer = new Time(100);
        public Timer(int seconds);
        public Timer(int minutes, int seconds);
        public Timer(int hours, int minutes, int seconds);
    }
}

}

如果可以,你能解释一下你是怎么做到的吗?

4

3 回答 3

1

指定方法的返回类型,或将方法名称从 更改TimerTime

即改变以下

    public Timer(int seconds);
    public Timer(int minutes, int seconds);
    public Timer(int hours, int minutes, int seconds);

    public Time(int seconds);
    public Time(int minutes, int seconds);
    public Time(int hours, int minutes, int seconds);
于 2013-11-14T11:31:31.317 回答
0

如果您想表示曲目的持续时间,请使用时间跨度...请参阅此处的示例...

TimeSpan TrackDuration= new TimeSpan(3, 01, 59);
Console.WriteLine(TrackDuration.ToString());      

例如(编辑):我已修改,因此您可以根据需要将秒作为 int 传递

public class Track
        {
            internal enum Category
            {
                Ambient, Blues, Country, Disco, Electro,  Hardcore,
                HardRock, HeavyMetal, Hiphop, Jazz, Jumpstyle, Klassiek, Latin, 
                Other, Pop, Punk, Reggae, Rock, Soul, Trance, Techno
            }

            public Track(int id, string name, string artist, 
                         string albumSource, Category category, 
                          TimeSpan duration, int seconds)
            {
                Id = id;
                Name = name;
                Artist = artist;
                AlbumSource = albumSource;
                TrackCategory = category;
                Duration = duration;
                Seconds = seconds;
            }

            public TimeSpan Duration { get; set; }

            public Category TrackCategory { get; set; }

            public string AlbumSource { get; set; }

            public string Artist { get; set; }

            public string Name { get; set; }

            public int Id { get; set; }

            public int Seconds { get; set; }

            public override string ToString()
            {
            TimeSpan ts = TimeSpan.FromSeconds(Seconds);
            return string.Format("{0} by {1} is {2} long - 
                            using seconds {3:%m} Minutes {3:%s} Seconds", 
                                Name, Artist, Duration, ts);
              }
        }

然后像这样使用...

var track = new Track(1, "Song 1", "Artist name", "Album name", 
             Track.Category.Blues, new TimeSpan(0, 0, 3, 59), 100);

Console.WriteLine(track.ToString());
Console.ReadLine();
于 2013-11-14T11:36:19.503 回答
0

我建议您使用 aTimeSpan而不是创建新类型来保存持续时间。IFormattable您可以通过使用以下类似方法来避免导致 TimeSpan (未实现)的问题DateTime

public class Track
{
    public int      Id       { get; set; }
    public string   Name     { get; set; }
    public TimeSpan Duration { get; set; }

    public override string ToString()
    {
        return String.Format("{0}. {1} ({2:mm'm'ss's'})",
             this.Id, this.Name, DateTime.MinValue.Add(this.Duration));
    }
}

static void Main(string[] args)
{
    var tracks = new[]
    { 
        new Track { Id = 1, Name = "Song #1", Duration = TimeSpan.Parse("00:01:30") /* 01m30s */ },
        new Track { Id = 2, Name = "Song #2", Duration = TimeSpan.FromSeconds(45)   /* 00m45s */ },
        new Track { Id = 3, Name = "Song #3", Duration = new TimeSpan(0, 2, 45)     /* 02m45s */ }
    };

    foreach (var track in tracks)
        Console.WriteLine(track);
}
于 2013-11-14T11:48:25.183 回答