Jake
Jake

Reputation: 29

How to Restart Program at End of Code in C# Console?

I'm working on a exercise where you develop a console app that you enter a speed limit and speed and then it tells you if over, how much, and how many demerit points per every 5kmh over. (12 demerit point is license suspended)

I am wondering how I would restart the code back to the beginning to calculate another speed?

static void Main(string[] args)
{
    Console.Write("Enter the speed limit:  ");
    var speedLimit = Convert.ToInt32(Console.ReadLine());
    Console.Write("Enter the car speed:  ");
    var carSpeed = Convert.ToInt32(Console.ReadLine());
    const int demeritPoint = 5;
    var pointTotal = (carSpeed - speedLimit) / demeritPoint;
    if (speedLimit >= carSpeed)
        Console.WriteLine("Ok");
    else if (speedLimit < carSpeed && pointTotal < 12)
    {
        Console.WriteLine("Speed is" + " " + (carSpeed - speedLimit) + "kmh " + "Over");
        Console.WriteLine("");
        Console.WriteLine(pointTotal + " " + "Deremit Points");
    }
    else if (speedLimit < carSpeed && pointTotal >= 12)
    {
        Console.WriteLine("Speed is" + " " + (carSpeed - speedLimit) + "kmh " + "Over");
        Console.WriteLine("");
        Console.WriteLine(pointTotal + " " + "Deremit Points");
        Console.WriteLine("");
        Console.WriteLine("SUSPENDED");
    }
    Console.Write("Press enter to clear:");
    Console.ReadLine();
}

Upvotes: 1

Views: 1093

Answers (2)

mirazimi
mirazimi

Reputation: 880

string rerun="n" ;
do
  {
//your code
Console.WriteLine("Do you want to continue? y / n ");
rerun = Console.ReadLine();
 }
while (rerun == "y");

Upvotes: 0

Ipsit Gaur
Ipsit Gaur

Reputation: 2927

You don't need to restart your application for this, running your code into loop will solve your problem.

static void Main(string[] args)
{
  while(true) {
    // Complete piece of code
  }
}

Instead of using true in the condition, you can add terminating condition for your application.

Upvotes: 1

Related Questions