c# - If and Switch statements; Is there an easier way of producing this code? -
my code follows:
namespace calculation { class program { static void main(string[] args) { console.writeline("this system calculate speed, distance or time."); console.writeline("1 = speed - 2 = distance - 3 = time"); console.writeline("please enter calculation perform. 1, 2 or 3"); string usercalculation = console.readline(); int calculation = int.parse(usercalculation); if(calculation < 1) { console.writeline("please enter number greater or equal 1 less 3."); } if (calculation > 3) { console.writeline("please enter number less 3 greater or equal 1."); } else { switch (calculation) { //this statement calculates speed. case 1: console.writeline("you have chose calculate speed. s = d/t"); console.writeline("to work out need firstly enter distance in metres"); string userdistance = console.readline(); int distance = int.parse(userdistance); console.writeline("now enter time in seconds."); string usertime = console.readline(); int time = int.parse(usertime); console.writeline("your speed " + distance / time + " m/s"); console.writeline("in mph " + distance / time * 2.23 + "mph"); break; //this statement calculates distance. case 2: console.writeline("you have chose calculate distance. d = sxt"); console.writeline("to work out need firstly enter speed"); string userspeed = console.readline(); int speed = int.parse(userspeed); console.writeline("now enter time in hours."); string usertime1 = console.readline(); double time1 = double.parse(usertime1); console.writeline("your distance " + speed * time1 + " miles"); break; //this statement calculates time. case 3: console.writeline("you have chose calculate time. t = d/s"); console.writeline("to work out need firstly enter distance in miles."); string usermiles = console.readline(); int miles = int.parse(usermiles); console.writeline("now enter speed in mph."); string userspeed2 = console.readline(); double speed2 = double.parse(userspeed2); console.writeline("your time " + miles / speed2 + "hours."); console.writeline("this " + miles / speed2 * 60 + " minutes"); break; } } } } }
you can remove 1 if statement not diffrent die other if statement. replace one:
if (calculation < 1 || calculation > 3) { console.writeline("please enter number greater or equal 1 less 3."); }
create 2 funtions return user input 1 int , second 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 should move calculations funtion can use enum better readability.
enum options { speed, time, distance } //example options calculation = (options)calculation; //cast user input number // option enum switch (calculation) { case options.distance: // code break; case options.speed: // code break; }
Comments
Post a Comment