Search code examples
c#if-statementwhile-loopdo-whileapplication-restart

Restart the codeblock with a while-loop


Problem: I would like to restart my codeblock after the error message.

Console.Write("\tBitte geben Sie ihre erste Zahl ein: ");

if (!double.TryParse(Console.ReadLine(), out zahl1))
    Console.WriteLine("\tUngültige Eingabe. Bitte geben Sie nur Zahlen an!");

Solution

  • You can just change your if into a while:

    Console.Write("\tBitte geben Sie ihre erste Zahl ein: ");
    while (!double.TryParse(Console.ReadLine(), out zahl1))
        Console.WriteLine("\tUngültige Eingabe. Bitte geben Sie nur Zahlen an!");