Click here to Skip to main content
15,881,139 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hello!

I am new to programming and am experiencing issues with a the following exercise:

class program
{
public static void Main(string[] args)
{
string n=Calculator.WriteNumber(53+28);
Console.WriteLine(n);

Console.ReadLine();

I am confused by the (53 +28) part.How do I add an arithmetic sign?

What I have tried:

I created the following class which compiles( when Calculator.WriteNumber(53,28);)


class Calculator
{

public static string WriteNumber(int a ,int b)
{
int c = a + b;
string result = c.ToString();
return result;
}
Posted
Updated 10-Feb-18 5:32am

53+28 counts as one parameter (so you still have to supply a second one): when you call WriteNumber, it will take 81 (i.e. 53 + 28) as parameter a. It does not get differently treated because there is an arithmetic sign: 53 + 28 evaluates to 81 which is one int and here you pass it as parameter a.
 
Share this answer
 
Comments
Member 13671506 10-Feb-18 10:47am    
Thank you for your reply. If it is treated as one parameter , how do I add a second one without changing the look of string n=Calculator.WriteNumber(53+28)?
Dave Kreskowiak 10-Feb-18 10:50am    
Because your WriteNumber method takes two parameters, you have to supply the parameters with a comman between them. Are you looking for:
    string n = Calculator.WriteNumber(53, 28);
Maciej Los 10-Feb-18 10:48am    
Well explained.
Thomas Daniels 10-Feb-18 10:50am    
Thank you!
Member 13671506 10-Feb-18 10:48am    
I can say string n= Calculator.WriteNumber(53+87,0) but I don't think it will be accepted.
You could try
C#
string n = Calculator.WriteNumber(53, 28);

instead.
The addition is realized by the WriteNumber method.
You can perform operations on method's parameters prior to passing them:
C#
string n = Calculator.WriteNumber(10 - 5, 2 * 2);

This is equivalent to
C#
string n = Calculator.WriteNumber(5, 4);

and will return 9.
 
Share this answer
 
Comments
Member 13671506 10-Feb-18 10:56am    
The solution has to be in the format string n=Calculator.WriteNumber(52 + 28); and the result has to be 81. I submitted (53,28) but my teacher said that It has to be (52 + 28)
phil.o 10-Feb-18 11:06am    
Then you have to define your method with only one parameter, but then there is no point to have a method here since there is nothing to compute (you already computed the result while calling the method).
It is unclear what you (or your teacher) really expect:
- either you keep two arguments for your method but then you have to call the method with two arguments.
- or you give just one parameter to the method and call it with this single parameter, but as I said there is no point anymore to have a method then (since there is nothing more to compute).
To pass two parameters, you separate them with a comma, just as you do when you declare the method:
string n=Calculator.WriteNumber(53, 28);

If you want to pass an operator, then I'd do it with an enum:
C#
public enum Operators
   {
   Plus,
   Minus,
   Times,
   DivideBy,
   }

Then:
public static string Calculate(int a, Operators op, int b)
    {
    int result;
    switch (op)
        {
        case Operators.Plus: result = a + b; break;
        case Operators.Minus: result = a - b; break;
        case Operators.Times: result = a * b; break;
        case Operators.DivideBy: result = a / b; break;
        default: throw new ArgumentException("Unknown Operator: " + op);
        }
    return result.ToString();
    }
You then call it like this:
string n = Calculator.Calculate(53, Operators.Plus, 28);

Note that I changed the name of your method: don't call something "WriteNumber" unless it actually writes something!
 
Share this answer
 
public static string WriteNumber(int a ,int b=0)
string n=Calculator.WriteNumber(53+28);

The point of the exercise was for me to use default value and optional parameters.

This is the solution, thank you all for your help!
 
Share this answer
 
v3
Comments
PIEBALDconsult 10-Feb-18 13:51pm    
Please don't answer your own question.
Member 13671506 10-Feb-18 14:20pm    
OK

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900