Question calculate two numbers

Tsukeo

New member
Joined
Mar 12, 2014
Messages
2
Programming Experience
Beginner
Hi. I started last week trying to learn C#, and think I've gotten a good way already. I learned about classes today! :D

I plan on one day being a game developer. :)

Anyway, I need some help with a calculator I tried to make in Command Console(?), when I type in the two numbers, it won't calculate, the answer is blank!
Can someone help me? (Sorry if this seems like a stupid question.)

C#:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;


namespace ConsoleApplication9
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Write your first number:");
            string numOne = Console.ReadLine();
            Console.WriteLine("Write your second number: ");
            string numTwo = Console.ReadLine();


            int numberOne = Convert.ToInt32(numOne);
            int numberTwo = Convert.ToInt32(numTwo);


            Console.WriteLine("+, -, / or *?");
            string method = Console.ReadLine();


            switch (method)
            {
                case "+":
                    Plus(numberOne, numberTwo);
                    Console.ReadLine();
                    break;
                case "-":
                    Minus(numberOne, numberTwo);
                    break;
                    Console.ReadLine();
                    break;
                case "*":
                    Multiply(numberOne, numberTwo);
                    Console.ReadLine();
                    break;
                case "/":
                    Division(numberOne, numberTwo);
                    Console.ReadLine();
                    break;
                default:
                    Console.WriteLine("Error!");
                    break;
            }
        }


        static int Multiply(int a, int b)
        {
            int answer = a * b;
            return answer;
        }


        static int Minus(int a, int b)
        {
            int answer = a - b;
            return answer;
        }


        static int Plus(int a, int b)
        {
            int answer = a + b;
            return answer;
        }


        static double Division(int a, int b)
        {
            if (a == 0) // This is just so that I won't get an error!
            {
                return 0;
            }
            else if (b == 0)
            {
                return 0;
            }
            else
            {
                double answer = a / b;
                return answer;
            }


        }
    }
}
 

jmcilhinney

C# Forum Moderator
Staff member
Joined
Apr 23, 2011
Messages
3,272
Location
Sydney, Australia
Programming Experience
10+
You're never actually writing out the answer. You call one of your calculation methods and they return the result but you never actually use that result. You have to call Console.WriteLine to write that result out so the user can see it.
 
Top Bottom