请在主要说明c sharp sum方法实现 [英] please explain c sharp sum method implementation in main

查看:74
本文介绍了请在主要说明c sharp sum方法实现的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

namespace CommandLine
{
    class Program
    {
        int a = 10, b = 10, c;
        public void add()
        {
            c = a + b;
            
        }
        static void Main(string[] args)
        {
            
            Console.WriteLine("additon:",c);
           
        }
    }
}



//显示错误我是.Net的新手请说明如何添加两个数字和打印总和


//shows error i am new to .Net please explain how to add two numbers and print sum

推荐答案

您需要调用 add 方法来获取结果。您还需要学习如何使用参数格式化字符串,以及如何在方法中使用参数。我强烈建议你买一本关于C#的书,并通过它来学习基础知识。



.Net Book Zero [ ^ ]是一个非常好的在线入门。
You need to call the add method to get the result. You also need to learn how to format strings with parameters, and how to use parameters in methods. I would strongly suggest you get a book on C# and work through it to learn the basics.

.NET Book Zero[^] is a very good online primer.


首先,显示错误,我是.Net的新手请解释不会是问题的陈述。一个简单的 Google [ ^ ]搜索会为您提供数百万的结果,所以您可能不会试试吧。



现在第二件事,由 ThePhantomUpvoter abbaspirmoradi 提供的建议非常好,你可以按照那样做。我只是看到你的另一种方法,只用2行代码就可以添加2个整数。参见:

First thing, "shows error i am new to .Net please explain" would not be a statement of a problem. A simple Google[^] search would provide you millions of result,so you probably don't try that.

Now the second thing, suggestions provided by ThePhantomUpvoter and abbaspirmoradi are quite good,you can follow that.I just see you another way to do it with just 2 lines of code that could add 2 integer numbers.See:
class Program
{
    //int a = 10, b = 10, c;
    public static int add(int a, int b)
    {
        return a + b;
    }
    static void Main(string[] args)
    {
        Console.WriteLine("additon:" + add(10,10));
    }
}



所以我将 void add()方法更改为 static int 这样它就可以返回一个值,只需在 Main 中调用它来打印结果。



干杯!

Shuvro


So i changed your void add() method to static int so that it can return a value and just call it inside the Main to print the result.

Cheers!
Shuvro


我只是更正你的代码。这很容易。但我的朋友,如果你是新的主要的第一次尝试和尝试。这个

很清楚,如果你在第一次有错误。不用担心。很多书存在试试。



看看这是:



http://www.apress.com/9781430210337/ [ ^ ]



http:// freecomputerbooks .com / Programming-C-Sharp-for-Beginners.html [ ^ ]





I just correct your code.this was easy.but my friend if you new in major first try and try .this
is clear if you have got error at first.don't worry.many book exist try with them.

look at this are:

http://www.apress.com/9781430210337/[^]

http://freecomputerbooks.com/Programming-C-Sharp-for-Beginners.html[^]


namespace CommandLine
{
    class Program
    {
     
 static int c = 0, a = 10, b = 10;

        public static int add(int a1, int b1)
        {
            return c = a1 + b1;
        }

        private static void Main(string[] args)
        {
            c = add(a, b);
            Console.WriteLine("additon:{0}", c);
            Console.ReadLine();

        }
    }
}


这篇关于请在主要说明c sharp sum方法实现的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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