-3

I have to create a class Calculator with the method WriteNumber that is a string, that can calculate two numbers. I really don't know how to do it I tried to do something by creating a class just to convert the two first numbers but with no results. Any help would be good.

class Program
{
    static void Main(string[] args)
    {
        string n = Calculator.WriteNumber(5+10);
        Console.WriteLine(n);
        
        Console.ReadLine();
    }
}
  • Maybe you should show that WriteNumber method? – Hans Kesting Dec 12 '20 at 14:09
  • Does this answer your question? [String calculator](https://stackoverflow.com/questions/21950093/string-calculator) –  Dec 12 '20 at 14:11
  • Did you mean evaluating an expression given as string (e.g. a user input)? `string n = Calculator.WriteNumber("5+10");`. If yes, you must specify what kind of calculation you want to be able to perform. Probably the 4 basic operations `+ - * /`? With operator precedence (i,e,. `* /` before `+ -`)? – Olivier Jacot-Descombes Dec 12 '20 at 14:23
  • public static string WriteNumber(int number1 = 0, int number2 = 0) – Stefan Stojanović Dec 12 '20 at 14:49
  • `public static string WriteNumber(int number1, int number2) {return number1+number2;}` the thing that helped me was changing `int number1` to `int number1 = 0` same with two. And with the help of the answer below adding `.ToString` – Stefan Stojanović Dec 12 '20 at 14:55

2 Answers2

1

Your function doesn't do the calculation in your code.

Your main should look like this:

class Program
{
    static void Main(string[] args)
    {
        string n = Calculator.WriteNumber(5, 10);
        Console.WriteLine(n);
        
        Console.ReadLine();
    }
}

And the calculator class could look like this:

static class Calculator
{
    static string WriteNumber(int num1, int num2)
    {
        return (num1 + num2).ToString();
    }
}

Though I will recommend you to change the function name to a more intuitive one.

TheSiIence
  • 327
  • 2
  • 10
0
using System;
                    
    class Program
    {
        static void Main(string[] args)
        {
            var n = Calculator(5,10);
            Console.WriteLine(n.ToString());
            
            Console.ReadLine();
        }
        
        static int Calculator (int n1, int n2)
        {
          return n1 + n2;
        }    
    }
Useme Alehosaini
  • 2,998
  • 6
  • 18
  • 26
  • No worries :) @Stefan Stojanović – Useme Alehosaini Dec 12 '20 at 15:02
  • @Stefan Stojanović , while you are learning, I recommend that you should learn the right way to handle problems. For example, the Calculator should **NEVER** return a string, this is against every logic. Calculator, if it is general, should return `decimal`, or if its function to add two integers, should return an `integer`, Then if you like to **print it** or use it somewhere else only that time you should **convert** it to another type (in your case `ToString()`) – Useme Alehosaini Dec 12 '20 at 15:08
  • Yea I know but it was for an assignment at my school. I was also a bit confused when they gave it to me. – Stefan Stojanović Dec 12 '20 at 19:31