Grupo
Usando bibliotecas adicionales en C#
Objectivo
1. Usa DateTime.Now para obtener la hora actual.
2. Usa Console.SetCursorPosition para mover el cursor a la esquina superior derecha de la pantalla.
3. Muestra la hora actual en el formato HH:mm:ss.
4. Usa un bucle para actualizar la hora cada segundo.
5. Asegúrate de borrar la hora anterior antes de actualizar para evitar solapamientos.
Crea un programa que muestre la hora actual en la esquina superior derecha de la pantalla con el formato 12:52:03. El programa debe pausarse un segundo y luego mostrar la hora nuevamente en la misma ubicación.
Ejemplo de ejercicio en C#
Mostrar código C#
using System; // Importing the System namespace for basic functionalities
using System.Threading; // Importing the System.Threading namespace for Thread.Sleep()
class Program
{
static void Main()
{
// Infinite loop to keep updating the time every second
while (true)
{
// Get the current time
DateTime currentTime = DateTime.Now;
// Move the cursor to the top-right corner of the console
Console.SetCursorPosition(Console.WindowWidth - 9, 0);
// Clear the previous time by writing spaces over it
Console.Write(" ");
// Display the current time in the format HH:mm:ss
Console.SetCursorPosition(Console.WindowWidth - 9, 0);
Console.Write(currentTime.ToString("HH:mm:ss"));
// Pause for 1 second before updating the time again
Thread.Sleep(1000);
}
}
}
Output
(Displayed at the top-right corner of the screen)
12:52:03
12:52:04
12:52:05
12:52:06
...
Código de ejemplo copiado
Comparte este ejercicio de C#