Refactoring
Now, we have to find another way to make our code simpler in terms of code and performance. The code shown below.
using System;
namespace ConsoleApp1
{
class Program
{
static void Main()
{
string entry;
bool keepGoing = true;
double totalWorkingTime = 0;
while (keepGoing){
// Prompt the for minutes exercised
Console.WriteLine("Enter how many minutes you exercised: ");
entry = Console.ReadLine();
if (entry.toLower() == "quit")
{
keepGoing = false;
}
else
{
try()
{
double minutes = int.Parse(entry);
if (minutes <= 0)
{
Console.Write("Not a desired input");
continue;
}
else if (minutes <= 10)
{
Console.WriteLine("Better than nothing, am I right?");
}
else if (minutes <= 30)
{
Console.WriteLine("Press your limit!");
}
else if (minutes <= 60)
{
Console.WriteLine("You must be an Indonesia ninja warrior");
}
else
{
Console.WriteLine("Okay, now you're just showing off!");
}
}
catch(FormatException)
{
Console.WriteLine("Not a valid input, try again");
continue;
}
// Add minutes exercised to a running total
totalWorkingTime = totalWorkingTime + minutes;
// Display the running total in minutes
Console.Write("Your total working minutes is " + totalWorkingTime + " minutes");
}
}
}
}
}
We could improve some code like:
Removing keepGoing variable and turn it into
if (entry.toLower() == "quit")
{
break;
}
while (true){
}