accept only numbers
-
i want my code to accept only numbers as the input in this calculator code.....how can i do that class Program { int operand1; int operand2; char oper; int result; public void Display() { Console.WriteLine("Enter the value of operand1"); if (operand1 < 48 && operand2 > 57) { Console.WriteLine(""); } operand1 = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter the value for operand2"); operand2 = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter the oper"); oper = Convert.ToChar(Console.ReadLine()); } public void Result() { switch (oper) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; } } public void Displayresult() { Console.WriteLine("{0} ", result); Console.ReadLine(); } public static void Main() { Program obj = new Program(); obj.Display(); obj.Result(); obj.Displayresult(); } } } thanks
C#
-
i want my code to accept only numbers as the input in this calculator code.....how can i do that class Program { int operand1; int operand2; char oper; int result; public void Display() { Console.WriteLine("Enter the value of operand1"); if (operand1 < 48 && operand2 > 57) { Console.WriteLine(""); } operand1 = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter the value for operand2"); operand2 = Int32.Parse(Console.ReadLine()); Console.WriteLine("Enter the oper"); oper = Convert.ToChar(Console.ReadLine()); } public void Result() { switch (oper) { case '+': result = operand1 + operand2; break; case '-': result = operand1 - operand2; break; } } public void Displayresult() { Console.WriteLine("{0} ", result); Console.ReadLine(); } public static void Main() { Program obj = new Program(); obj.Display(); obj.Result(); obj.Displayresult(); } } } thanks
C#
Use Int.TryParse() in order to check if the string is an integer
#region signature my articles #endregion
-
Use Int.TryParse() in order to check if the string is an integer
#region signature my articles #endregion
-
You should use it instead of Int.Parse() Have a look at documentatio in MSDN, it shows how to use Int.TryParse()
#region signature my articles #endregion