定时器类...不更新

时间:2009-12-20 21:41:27

标签: c#

嗨我制作了这个代码,它会在运行时钟时显示,然后要求用户输入新的秒数,分钟数和小时数,这样它们就会被添加到我做过的旧时钟中,但结果是错误的,哪里是错误的

主要

static void Main(string[] args)
    {
        Timer S1 = new Timer();
        S1.Sec = 10;
        S1.Min = 25;
        S1.Hour = 23;
        S1.Read();
        S1.Write();
    }

计时器类

namespace Uni_19
{
class Timer
{
    private int Seconds;
    private int Minutes;
    private int Hours;
    private int Days = 0;

    public int Sec
    {
        get { return Seconds; }
        set
        {
            Seconds = value % 60;
            Minutes += value / 60;
        }
    }
    public int Min
    {
        get { return Minutes; }
        set
        {
            Minutes = value % 60;
            Hour += (value / 60);
        }
    }
    public int Hour
    {
        get { return Hours; }
        set 
        {
            Hours = value % 24;
            Days += value / 24;
        }
    }
    public int Day
    {
        get { return Days; }
        set { Days = Hour / 24; }
    }
    public int AddSec(int A)
    {
        Sec += A;
        return Sec;
    }
    public int AddMin(int B)
    {
        Min += B;
        return Min;
    }
    public int AddHour(int C)
    {
        Hour = Hour + (C % 24);
        return Hour;
    }
    public int AddDay(int D)
    {
        Day += D;
        return Day;
    }
    public void Read()
    {
        Console.WriteLine("Time Is: {0}", Hours + " : " + Minutes + " : " + Seconds);
        Console.Write("Time Added (in Seconds): ");
        Sec = int.Parse(Console.ReadLine());
        Console.Write("Time Added (in Minutes): ");
        Min = int.Parse(Console.ReadLine());
        Console.Write("Time Added (in Houres): ");
        Hour = int.Parse(Console.ReadLine());
    }
    public void Write()
    {
        Console.WriteLine("New Time Is: {0}", AddDay(Day) + " : " + AddHour(Hour) + " : " + AddMin(Min) + " : " + AddSec(Sec));
    }
}
}
提前

thanx

4 个答案:

答案 0 :(得分:2)

您正在使用设置当天和添加的用户输入。旧时间被完全覆盖,不用于计算。

Sec = int.Parse(Console.ReadLine());  // Calls the setter 'Sec' which overwrites Seconds.

最重要的是,AddSec例程可以返回一个大于60的数字,这可能不是你想要的。

您的家庭作业的规格是什么?您的Timer类在.NET中是完全不必要的,因为它提供的功能已经被DateTime和TimeSpan类所涵盖。我假设你不允许这样做,否则会让你的任务太容易了?

可能的解决方法,将Sec =更改为AddSec等等:

Console.Write("Time Added (in Seconds): ");
AddSec(int.Parse(Console.ReadLine()));
Console.Write("Time Added (in Minutes): ");
AddMin(int.Parse(Console.ReadLine()));
Console.Write("Time Added (in Houres): ");
AddHour(int.Parse(Console.ReadLine()));

...

Console.WriteLine("New Time Is: {0}", Day + " : " + Hour + " : " + Min + " : " + Sec);

完整源代码:

using System;

namespace Uni_19
{
    class Timer
    {
        private int Seconds;
        private int Minutes;
        private int Hours;
        private int Days = 0;

        public int Sec
        {
            get { return Seconds; }
            set
            {
                Seconds = value % 60;
                Minutes += value / 60;
            }
        }
        public int Min
        {
            get { return Minutes; }
            set
            {
                Minutes = value % 60;
                Hour += (value / 60);
            }
        }
        public int Hour
        {
            get { return Hours; }
            set
            {
                Hours = value % 24;
                Days += value / 24;
            }
        }
        public int Day
        {
            get { return Days; }
            set { Days = Hour / 24; }
        }
        public int AddSec(int A)
        {
            Sec += A;
            return Sec;
        }
        public int AddMin(int B)
        {
            Min += B;
            return Min;
        }
        public int AddHour(int C)
        {
            Hour = Hour + (C % 24);
            return Hour;
        }
        public int AddDay(int D)
        {
            Day += D;
            return Day;
        }
        public void Read()
        {
            Console.WriteLine("Time Is: {0}", Hours + " : " + Minutes + " : " + Seconds);
            Console.Write("Time Added (in Seconds): ");
            AddSec(int.Parse(Console.ReadLine()));
            Console.Write("Time Added (in Minutes): ");
            AddMin(int.Parse(Console.ReadLine()));
            Console.Write("Time Added (in Houres): ");
            AddHour(int.Parse(Console.ReadLine()));
        }
        public void Write()
        {
            Console.WriteLine("New Time Is: {0}", Day + " : " + Hour + " : " + Min + " : " + Sec);
        }
    }

    class Program
    {
        static void Main(string[] args)
        {
            Timer S1 = new Timer();
            S1.Sec = 10;
            S1.Min = 25;
            S1.Hour = 23;
            S1.Read();
            S1.Write();
        }
    }
}

答案 1 :(得分:0)

你应该考虑使用DateTime结构而不是重新发明它

答案 2 :(得分:0)

这不是一个真正的答案,而是更多的建议:

对于Write()方法,尝试使用ToString或将代码更改为更容易的方法:

Console.WriteLine("New Time Is: {0} : {1} : {2} : {3}",AddDay(Day),AddHour(Hour),AddMin(Min),AddSec(Sec));

将来可能会让您更容易改变。

为什么你决定使用自己的Timer类,是否不适合使用提供的DateTime类?

答案 3 :(得分:0)

这种方法对我来说似乎很可疑,这意味着什么?

public void Write()
{
    Console.WriteLine("New Time Is: {0}", AddDay(Day) + " : " + AddHour(Hour) + " : " + AddMin(Min) + " : " + AddSec(Sec));
}

在封装字段时也请使用一些约定

private int _seconds
public int Seconds {get;set;}