之前学过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);
}
}
}
}