Beispiel #1
0
        static void Main(string[] args)
        {
            bool closeApp = false;

            while (!closeApp)
            {
                // Declare variables and set to empty.
                var    input1 = "";
                var    input2 = "";
                double result = 0;

                // first number
                Console.Write("Type a number, and then press Enter: ");
                input1 = Console.ReadLine();

                double int1 = 0;
                while (!double.TryParse(input1, out int1))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    input1 = Console.ReadLine();
                }

                // second number.
                Console.Write("Type another number, and then press Enter: ");
                input2 = Console.ReadLine();

                double int2 = 0;
                while (!double.TryParse(input2, out int2))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    input2 = Console.ReadLine();
                }

                // choose operation
                Console.WriteLine("Choose an operator from the following list:");
                Console.WriteLine("\ta - Add");
                Console.WriteLine("\ts - Subtract");
                Console.WriteLine("\tm - Multiply");
                Console.WriteLine("\td - Divide");
                Console.Write("Your option? ");

                string op = Console.ReadLine();

                try
                {
                    result = Calculator.DoOperation(int1, int2, op);
                    if (double.IsNaN(result))
                    {
                        Console.WriteLine("This operation will result in a mathematical error.\n");
                    }
                    else
                    {
                        Console.WriteLine("Your result: {0:0.##}\n", result);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
                }

                Console.WriteLine("------------------------\n");

                Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
                if (Console.ReadLine() == "n")
                {
                    closeApp = true;
                }

                Console.WriteLine("\n");
            }
            return;
        }
Beispiel #2
0
        static void Main(string[] args)
        {
            bool endApp = false;

            // Display title as the C# console calculator app.
            Console.WriteLine("Console Calculator in C#\r");
            Console.WriteLine("------------------------\n");


            Calculator calculator = new Calculator();

            while (!endApp)
            {
                // Declare variables and set to empty.
                string numInput1 = "";
                string numInput2 = "";
                double result    = 0;

                // Ask the user to type the first number.
                Console.Write("Type a number, and then press Enter: ");
                numInput1 = Console.ReadLine();

                double cleanNum1 = 0;
                while (!double.TryParse(numInput1, out cleanNum1))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    numInput1 = Console.ReadLine();
                }

                // Ask the user to type the second number.
                Console.Write("Type another number, and then press Enter: ");
                numInput2 = Console.ReadLine();

                double cleanNum2 = 0;
                while (!double.TryParse(numInput2, out cleanNum2))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    numInput2 = Console.ReadLine();
                }

                // Ask the user to choose an operator.
                Console.WriteLine("Choose an operator from the following list:");
                Console.WriteLine("\ta - Add");
                Console.WriteLine("\ts - Subtract");
                Console.WriteLine("\tm - Multiply");
                Console.WriteLine("\td - Divide");
                Console.Write("Your option? ");

                string op = Console.ReadLine();

                try
                {
                    result = calculator.DoOperation(cleanNum1, cleanNum2, op);

                    if (double.IsNaN(result))
                    {
                        Console.WriteLine("This operation will result in a mathematical error.\n");
                    }
                    else
                    {
                        Console.WriteLine("Your result: {0:0.##}\n", result);
                    }
                }
                catch (Exception e)
                {
                    Console.WriteLine("Oh no! An exception occurred trying to do the math.\n - Details: " + e.Message);
                }

                Console.WriteLine("------------------------\n");

                // Wait for the user to respond before closing.
                Console.Write("Press 'n' and Enter to close the app, or press any other key and Enter to continue: ");
                if (Console.ReadLine() == "n")
                {
                    endApp = true;
                }

                Console.WriteLine("\n"); // Friendly linespacing.
            }
            return;
        }
        static void Main(string[] args)
        {
            bool closeApp = false;

            Console.WriteLine("Console calculator in c#\r");
            Console.WriteLine("------------------------\n");

            Calculator calculator = new Calculator();

            while (!closeApp)
            {
                string nInput1 = "";
                string nInput2 = "";
                double result  = 0;

                Console.Write("Enter first number and then press Enter Key: ");
                nInput1 = Console.ReadLine();

                double cleanN1 = 0;
                while (!double.TryParse(nInput1, out cleanN1))
                {
                    Console.Write("This is not valid input. Please enter an integer value: ");
                    nInput1 = Console.ReadLine();
                }

                Console.Write("Enter second number and then press Enter Key: ");
                nInput2 = Console.ReadLine();

                double cleanN2 = 0;
                while (!double.TryParse(nInput2, out cleanN2))
                {
                    Console.Write("This is not a valid input. Please enter an integer value: ");
                    nInput2 = Console.ReadLine();
                }

                Console.WriteLine("Choose an mathematical operator from the list below:");
                Console.WriteLine("\tadd - Addition");
                Console.WriteLine("\tsub - Subtraction");
                Console.WriteLine("\tmul - Multiply");
                Console.WriteLine("\tdiv - Division");
                Console.WriteLine("Type your option");

                string opr = Console.ReadLine();

                try
                {
                    result = calculator.DoOperation(cleanN1, cleanN2, opr);
                    if (double.IsNaN(result))
                    {
                        Console.WriteLine("It will result in a mathematical error.\n");
                    }
                    else
                    {
                        Console.WriteLine("Answer is: {0:0.##}\n", result);
                    }
                }
                catch (Exception exp)
                {
                    Console.WriteLine("An exception occurred while doing to do the mathematical opration.\n - Details: " + exp.Message);
                }

                Console.WriteLine("-------------------------\n");

                Console.Write("Press 'c' and Enter to close the app, or press any other key and Enter to continue: ");
                if (Console.ReadLine() == "c")
                {
                    closeApp = true;
                }

                Console.WriteLine("\n");
            }
            calculator.Finish();
            return;
        }