lundi 12 septembre 2016

C# beginner. Using IF and Switch statements. Is there an easier way of producing this code?

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;
            }
        }

    }
}

}

Aucun commentaire:

Enregistrer un commentaire