Troy1 Troy1 - 2 months ago 7
C# Question

If and Switch statements; Is there an easier way of producing this code?

My code is as follows:

namespace Calculation
{
class Program
{
static void Main(string[] args)
{
Console.WriteLine("This is a system to calculate speed, distance or time.");
Console.WriteLine("1 = Speed - 2 = Distance - 3 = time");
Console.WriteLine("Please enter which calculation you would like to perform. 1, 2 or 3");

string userCalculation = Console.ReadLine();
int Calculation = int.Parse(userCalculation);

if(Calculation < 1)
{
Console.WriteLine("Please enter a number greater than or equal to 1 but less than 3.");
}

if (Calculation > 3)
{
Console.WriteLine("Please enter a number less than 3 but greater than or equal to 1.");
}

else
{
switch (Calculation)
{
//This statement calculates speed.
case 1:
Console.WriteLine("You have chose to calculate speed. S = D/T");

Console.WriteLine("To work this out you need to firstly enter your distance in metres");
string userDistance = Console.ReadLine();
int Distance = int.Parse(userDistance);

Console.WriteLine("Now enter your time in seconds.");
string userTime = Console.ReadLine();
int Time = int.Parse(userTime);

Console.WriteLine("Your speed is " + Distance / Time + " m/s");
Console.WriteLine("In MPH this is " + Distance / Time * 2.23 + "MPH");
break;

//This statement calculates distance.
case 2:
Console.WriteLine("You have chose to calculate distance. D = SxT");

Console.WriteLine("To work this out you need to firstly enter your speed");
string userSpeed = Console.ReadLine();
int Speed = int.Parse(userSpeed);

Console.WriteLine("Now enter your time in hours.");
string userTime1 = Console.ReadLine();
double Time1 = double.Parse(userTime1);

Console.WriteLine("Your Distance is " + Speed * Time1 + " miles");
break;

//This statement calculates time.
case 3:
Console.WriteLine("You have chose to calculate Time. T = D/S");

Console.WriteLine("To work this out you need to firstly enter your distance in miles.");
string userMiles = Console.ReadLine();
int Miles = int.Parse(userMiles);

Console.WriteLine("Now enter your Speed in MPH.");
string userSpeed2 = Console.ReadLine();
double Speed2 = double.Parse(userSpeed2);

Console.WriteLine("Your Time is " + Miles / Speed2 + "hours.");
Console.WriteLine("This would be " + Miles / Speed2 * 60 + " minutes");
break;
}
}

}
}
}

Answer

You can remove one if statement it is not very diffrent from die other if statement. replace it by this one:

        if (Calculation < 1 || Calculation > 3)
        {
            Console.WriteLine("Please enter a number greater than or equal to 1 but less than 3.");
        }

Create Two funtions to return user input one for int and the second for the double return type.

    private int GetIntUserInput()
    {
        string userInput = Console.ReadLine();
        int convertedUserInput = int.Parse(userInput);

        return convertedUserInput;
    }

    private double GetDoubleUserInput()
    {
        string userInput = Console.ReadLine();
        double convertedUserInput = double.Parse(userInput);

        return convertedUserInput;
    }

And I should move the calculations to a funtion as well You also can use an enum for better readability.

    enum Options
    {
        Speed,
        Time,
        Distance
    } 

     //example
      Options calculation = (Options)Calculation;
      switch (o)
      {
       case options.Distance:
           // some code
       break;
       case options.Speed:
           // some code
       break
      }