none
console RRS feed

  • Question

  • Hi everyone,

    I want to do a simple thing: the client will insert 2 numbers in console with a space between them and I want to do the sum of them.

    How can I do this?

    thank you!

    Wednesday, July 30, 2014 7:31 AM

Answers

  • Hi Shirly,

    you're talking about a Console-App? I think this is the wrong forum. But I've created one long time ago:

     static void Main(string[] args)
            {
               
                while (true)
                {
                    Console.WriteLine("Please enter two numbers: ");
                    Console.Write(">");
                    var line = Console.ReadLine();
                    var numberStrings = line.Split(' ');
                    if (numberStrings.Length != 2)
                    {
                        Console.WriteLine("Error: Please enter exactly two numbers (separated with a space)");
                        continue;
                    }
    
                    int numberOne;
                    int numberTwo;
                    if (!int.TryParse(numberStrings[0], out numberOne))
                    {
                        Console.WriteLine("Error: First value is not a valid number");
                        continue;
                    }
                    if (!int.TryParse(numberStrings[1], out numberTwo))
                    {
                        Console.WriteLine("Error: second value is not a valid number");
                        continue;
                    }
    
                    Console.WriteLine("Sum is {0}", numberOne + numberTwo);
                    break;
                }
    
                Console.ReadLine();
            }


    Thomas Claudius Huber

    "If you can't make your app run faster, make it at least look & feel extremly fast"

    My latest Pluralsight-course: Windows Store Apps - Data Binding in Depth

    twitter: @thomasclaudiush
    homepage: www.thomasclaudiushuber.com
    author of: ultimate Windows Store Apps handbook | ultimate WPF handbook | ultimate Silverlight handbook

    • Marked as answer by Shirly11 Wednesday, July 30, 2014 9:09 AM
    Wednesday, July 30, 2014 8:44 AM