Home > other >  How to make a number string decrement based on the value of the number
How to make a number string decrement based on the value of the number

Time:02-23

I want the the timeToString values change based on the count changing.So when the count of seconds decrement the second.Count.ToString("59") should decrement to "58".In the debug the PondoreCount function is working as expected, however I can't see the value decrementing in the console.


public class PonodoreTimer : Clock
    {
        private Clock _seconds;
        private Clock _minutes;

        public PonodoreTimer()
        {
            _seconds = new Clock();
            _minutes = new Clock();
            _seconds.Count = 59;
            _minutes.Count = 24;
            //   string timeToString = _minutes.Count.ToString("24")   ":"   _seconds.Count.ToString("59");
        }

      
        public string PondoreTimerInString()
        {
            string timeToString = _minutes.Count.ToString("24")   ":"   _seconds.Count.ToString("59");
            return timeToString;
        }
        
       

        public void PonodoreCount()
        {
            
            //string timeToString = _minutes.Count   ":"   _seconds.Count;

            //Console.WriteLine(timeToString);
            while (_minutes.Count != 0)
            {
               _seconds.Decrement();
                if(_seconds.Count == 0)
                {
                    _minutes.Decrement();
                    _seconds.Count = 59;
                }
            }
        }
    }

CodePudding user response:

The whole thing can be replaced by:

var ts = TimeSpan.FromMinutes(25);
var one = TimeSpan.FromSeconds(1);

while(ts > TimeSpan.Zero){
  ts = ts - one;
  Console.WriteLine($@"{ts:mm\:ss}");
}

24:59, 24:58 ...

If you want it to count down from 25:00, put the WriteLine before the subtract

CodePudding user response:

Change this string

 string timeToString = _minutes.Count.ToString("24")   ":"   _seconds.Count.ToString("59");

to this

string timeToString = _minutes.Count.ToString()   ":"   _seconds.Count.ToString();

or you always get "24" and "59"...

  •  Tags:  
  • c#
  • Related