添加参数的问题 [英] Problem with adding parameters

查看:105
本文介绍了添加参数的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

您好!



我是编程新手,遇到以下练习的问题:



class program

{

public static void Main(string [] args)

{

string n = Calculator.WriteNumber(53 + 28);

Console.WriteLine(n);



Console.ReadLine();



我对(53 +28)部分感到困惑。如何添加算术符号?



< b>我尝试了什么:



我创建了以下编译的类(当Calculator.WriteNumber(53,28);)





类计算器

{



公共静态字符串WriteNumber(int a,int b)

{

int c = a + b;

string result = c.ToString();

返回结果;

}

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;
}

推荐答案

5 3 + 28 计为一个参数(因此您仍然需要提供第二个参数):当您调用 WriteNumber 时,它将需要81(即53 + 28)作为参数 a 。它没有得到不同的处理,因为有一个算术符号: 53 + 28 评估为 81 这是一个 int 在这里你把它传递给参数 a
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.


你可以尝试

You could try
string n = Calculator.WriteNumber(53, 28);



代替。

增加是由 WriteNumber 方法。

您可以在传递方法参数之前对其执行操作:


instead.
The addition is realized by the WriteNumber method.
You can perform operations on method's parameters prior to passing them:

string n = Calculator.WriteNumber(10 - 5, 2 * 2);



这相当于


This is equivalent to

string n = Calculator.WriteNumber(5, 4);



并将返回 9


要传递两个参数,请用逗号分隔它们,就像声明方法时一样:

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:

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);



请注意,我更改了方法的名称:除非实际写入内容,否则不要调用WriteNumber!


Note that I changed the name of your method: don't call something "WriteNumber" unless it actually writes something!


这篇关于添加参数的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆