Return to start of program to retry

I teach myself C #, and the current task of the chapter asked me:

Take your last project and create additional methods that subtract, multiply, or divide the two numbers passed into it. In the division method, make sure that the second number is not equal to 0, since division by 0 is an illegal mathematical concept. If the second number is 0, return back 0.

Now I have written the following, which, I believe, meets all the criteria. I was not sure if the best option would be an IF expression. I also thought that SWITCH would also do the trick.

So the first question is: is IF or SWITCH better?

Second question. In ELSE, I give a general error message if the user does not select one of the available options, but what I would like to do is call ELSE (not sure what the correct term is), I want the program to go back to the beginning and ask the user to try again and display the first console. Writeline () asking you to select an operator. I know that this is not part of the problem, but it looks like a logical complement to the program, and I would like to know if this is possible without resorting to something too complicated.

Thanks in advance!

string whichOp; int firstNum, secondNum, result; Console.WriteLine("What Operator do you wish to use? [A]dd, [S]ubtract, [M]ultiply or [D]ivide?"); whichOp = Console.ReadLine(); whichOp = whichOp.ToLower(); if (whichOp == "a") { Console.Write("You chose Addition. Please choose your first number: "); firstNum = int.Parse(Console.ReadLine()); Console.Write("You chose the number {0}. Please choose a second number: ", firstNum); secondNum = int.Parse(Console.ReadLine()); result = Add(firstNum, secondNum); Console.WriteLine("You chose the number {0}. {1} plus {2} equals {3}.", secondNum, firstNum, secondNum, result); } else if (whichOp == "s") { Console.Write("You chose Subtraction. Please choose your first number: "); firstNum = int.Parse(Console.ReadLine()); Console.Write("You chose the number {0}. Please choose a second number: ", firstNum); secondNum = int.Parse(Console.ReadLine()); result = Sub(firstNum, secondNum); Console.WriteLine("You chose the number {0}. {1} minus {2} equals {3}.", secondNum, firstNum, secondNum, result); } else if (whichOp == "m") { Console.Write("You chose Multiplication. Please choose your first number: "); firstNum = int.Parse(Console.ReadLine()); Console.Write("You chose the number {0}. Please choose a second number: ", firstNum); secondNum = int.Parse(Console.ReadLine()); result = Mult(firstNum, secondNum); Console.WriteLine("You chose the number {0}. {1} times {2} equals {3}.", secondNum, firstNum, secondNum, result); } else if (whichOp == "d") { Console.Write("You chose Division. Please choose your first number: "); firstNum = int.Parse(Console.ReadLine()); Console.Write("You chose the number {0}. Please choose a second number: ", firstNum); secondNum = int.Parse(Console.ReadLine()); result = Div(firstNum, secondNum); Console.WriteLine("You chose the number {0}. {1} divided by {2} equals {3}.", secondNum, firstNum, secondNum, result); } else Console.WriteLine("FAIL! You did not choose an available option."); Console.ReadLine(); } static int Add(int num1, int num2) { int theAnswer; theAnswer = num1 + num2; return theAnswer; } static int Mult(int num1, int num2) { int theAnswer; theAnswer = num1 * num2; return theAnswer; } static int Sub(int num1, int num2) { int theAnswer; theAnswer = num1 - num2; return theAnswer; } static int Div(int num1, int num2) { int theAnswer; if (num2 == 0) return 0; theAnswer = num1 / num2; return theAnswer; } 

EDIT: I accepted the suggestions from them here and rebuilt the program using SWITCH and WHILE. Someone also said that since a lot of code is the same, I have to reuse it. I like this idea and I will see how I can do it.

  var retry = true; while (retry) { retry = false; string whichOp; int firstNum, secondNum, result; Console.WriteLine("What Operator do you wish to use? [A]dd, [S]ubtract, [M]ultiply or [D]ivide?"); whichOp = Console.ReadLine(); whichOp = whichOp.ToLower(); switch (whichOp) { case "a": Console.Write("You chose Addition. Please choose your first number: "); firstNum = int.Parse(Console.ReadLine()); Console.Write("You chose the number {0}. Please choose a second number: ", firstNum); secondNum = int.Parse(Console.ReadLine()); result = Add(firstNum, secondNum); Console.WriteLine("You chose the number {0}. {1} plus {2} equals {3}.", secondNum, firstNum, secondNum, result); Console.ReadLine(); break; case "s": Console.Write("You chose Subtraction. Please choose your first number: "); firstNum = int.Parse(Console.ReadLine()); Console.Write("You chose the number {0}. Please choose a second number: ", firstNum); secondNum = int.Parse(Console.ReadLine()); result = Sub(firstNum, secondNum); Console.WriteLine("You chose the number {0}. {1} minus {2} equals {3}.", secondNum, firstNum, secondNum, result); Console.ReadLine(); break; case "m": Console.Write("You chose Multiplication. Please choose your first number: "); firstNum = int.Parse(Console.ReadLine()); Console.Write("You chose the number {0}. Please choose a second number: ", firstNum); secondNum = int.Parse(Console.ReadLine()); result = Mult(firstNum, secondNum); Console.WriteLine("You chose the number {0}. {1} times {2} equals {3}.", secondNum, firstNum, secondNum, result); Console.ReadLine(); break; case "d": Console.Write("You chose Division. Please choose your first number: "); firstNum = int.Parse(Console.ReadLine()); Console.Write("You chose the number {0}. Please choose a second number: ", firstNum); secondNum = int.Parse(Console.ReadLine()); result = Div(firstNum, secondNum); Console.WriteLine("You chose the number {0}. {1} divided by {2} equals {3}.", secondNum, firstNum, secondNum, result); Console.ReadLine(); break; default: Console.WriteLine("I'm sorry. {0} is not an available option. Please try again.", whichOp.ToUpper()); retry = true; break; } } } static int Add(int num1, int num2) { int theAnswer; theAnswer = num1 + num2; return theAnswer; } static int Mult(int num1, int num2) { int theAnswer; theAnswer = num1 * num2; return theAnswer; } static int Sub(int num1, int num2) { int theAnswer; theAnswer = num1 - num2; return theAnswer; } static int Div(int num1, int num2) { int theAnswer; if (num2 == 0) return 0; theAnswer = num1 / num2; return theAnswer; } 
+4
source share
1 answer

(1) A switch is often a cleaner way of expressing this kind of branching. The main limitation of the switch is that it only works with constant compile-time values ​​(constant variables, literal strings, ints, enumerations, etc.). In this case, this does not seem to be a problem, so the switch is likely to be a good choice for cleaner, slightly shorter code. In performance-sensitive code (which, of course, is not), one switch can be faster than a bunch of ifs, because the program will check the value and jump right into the right case, and not check against each if condition until it matches.

(2) an easy way to do this is to wrap your entire program in a loop:

 var retry = true; while (retry) retry = false; // your program else { // or default: if you're going with switch ... retry = true; } } 
+4
source

Source: https://habr.com/ru/post/1434104/


All Articles