The article continued with the previous one
1. Declare the string type variable Symbol to save the selected operator
string Addition1, Addition2, Symbol;
2. Prompt to enter the operation Symbol to be performed in the console and receive it with Symbol
Console.WriteLine("Please input operation symbol:"); Symbol = Console.ReadLine();
3. Determine what operations need to be performed
if (Symbol == "+") { num3 = num1 + num2; } else if(Symbol=="-") { num3 = num1 - num2; } else if(Symbol=="/") { num3 = num1 / num2; } else if(Symbol=="*") { num3 = num1 * num2; }
4. The overall code is as follows. Because num3 is put into if judgment, it returns the error that num3 is used when it is not assigned, so we assign an initial value of 0 to num3 when declaring variables
string Addition1, Addition2, Symbol; int num1, num2, num3=0; Console.WriteLine("Please enter the first parameter:"); Addition1 = Console.ReadLine(); Console.WriteLine("Please enter the second parameter:"); Addition2 = Console.ReadLine(); Console.WriteLine("Please input operation symbol:"); Symbol = Console.ReadLine(); num1 = int.Parse(Addition1); num2 = int.Parse(Addition2); if (Symbol == "+") { num3 = num1 + num2; } else if(Symbol=="-") { num3 = num1 - num2; } else if(Symbol=="/") { num3 = num1 / num2; } else if(Symbol=="*") { num3 = num1 * num2; } Console.WriteLine("The result is:"); Console.WriteLine(num3.ToString()); Console.ReadKey();