1

At the moment I have made a timer that outputs on multiple lines like this:enter image description here

However, if the user chooses 1000 seconds that would take too much space.

I need a way so that it changes the first line to the number below automatically.

Here is my timer code:

public class TimerExample
{
    static int UserInputs()
    {
        int numberOfSeconds;
        do
        {
            Console.WriteLine("How many seconds would you like the test to be? Please type a number divisible by 10!");
            int.TryParse(Console.ReadLine(), out numberOfSeconds);
        } while (numberOfSeconds % 10 != 0);
        return numberOfSeconds;
    }
    public class TimerClass
        {
            public static int Timers(int timeLeft)
            {
                do
                {
                    Console.WriteLine($"timeLeft: {timeLeft}");
                    timeLeft--;
                    Thread.Sleep(1000);
                } while (timeLeft > 0);
                return timeLeft;
            }
        }
    public static void Main(string[] args)
    {
        int numberOfSeconds = UserInput();
        TimerClass.Timers(numberOfSeconds);
    }
}

Here is my full code if you need it: https://github.com/CrazyDanyal1414/mathstester

Community
  • 1
  • 1
d51
  • 316
  • 1
  • 6
  • 23

2 Answers2

1

If you want the count down to be shown on the same line then you can change the below line

Console.WriteLine($"timeLeft: {timeLeft}");

to

Console.Write("\rtimeLeft: {0}   ", timeLeft);

Hopefully that does the trick for you. \r will re-write the same line

Pankaj N
  • 26
  • 2
1

The key is to use:

  1. Console.Write and not Console.WirteLine
  2. Use "\r" at the start of your print so you override the line instead of append the data to the line

Here some code to test:


for (int i = 0; i < 1000; i++)
{
    Console.Write("\r" + i);
    Thread.Sleep(100);
}
Adrian Efford
  • 473
  • 3
  • 8