c# | 复习下方法的内容

之前学过python,方法这一块用起来差不多,就贴下程序好了
当然,学到了c#命名方法,
myVariable 这样命名用于变量名
MyVariable 这样命名用于方法名

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            int x = c.Add(3, 4);
            Console.WriteLine(x);

            string str = c.GetToday();
            Console.WriteLine(str);

            c.PrintSum(1, 2);


        }
    }

    class Calculator
    {
        public int Add(int a, int b)//加public可以让类外边也可以访问这个方法
        {
            int result = a + b;
            return result;
        }

        public string GetToday()
        {
            int day = DateTime.Now.Day;
            return day.ToString();
        }

        public void PrintSum(int a,int b)
        {
            int result = a + b;
            Console.WriteLine(result);
        }
    }
}

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            c.PrintXTo1(100);


        }
    }

    class Calculator
    {
        public void PrintXTo1(int x)
        {
            int r = 0;
            for (x = 0; x < 100; x++)
            {
                r = r + x;
                Console.WriteLine(r);

            }
        }
    }
}

递归

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            c.PrintTo1(100);


        }
    }

    class Calculator
    {
        public void PrintTo1(int x)
        {
            if (x==1)
            {
                Console.WriteLine(x);
            }
            else
            {
                Console.WriteLine(x);
                PrintTo1(x - 1);
            }
        }
    }
}

汉诺塔:
在吃饭的时候想通了汉诺塔问题

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace ConsoleApplication5
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator f = new Calculator();
            char a = 'a';
            char b = 'b';
            char c = 'c';
            f.PrinTol(10, a, b, c);


        }
    }

    class Calculator
    {
        public void PrinTol(int x,char a,char b,char c)
        {
            if(x == 1)
            {

                Console.WriteLine("{0}"+"->"+"{1}",a,c);                        
            }
            else
            {
                PrinTol(x - 1, a, c, b);
                PrinTol(1, a, b, c);
                PrinTol(x - 1, b, a, c);
            }
        }
    }                  
    
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值