005 C#词法、类型、变量、方法与简介
目录
*构成C#语言的基本元素(标记 token)
1)关键字(keyword)
2)操作符(operator)
3)标识符(identifier)
4)标点符号
5)文本
6)注释与空白
*简要介绍类型、变量与方法
*算法简介
构成C#语言的基本元素
1)关键字
2)操作符
3)标识符
*什么是合法的标识符
怎样阅读语言定义文档:
如果要用关键字,一定要加个@符号
*大小写规范
*命名规范
C# 方法:骆驼法(驼峰法),类:Parsal法
4)标点符号
5)文本(字面值)
*整数(int+long) int x=3; long y=3L;
多种后缀
*实数 float(32位) x=3.0f; double(64位) x=4.0D;
多种后缀
*字符 char c='a';这是单引号
*字符串 string str="a";这是双引号
*布尔值
*空值 string str=null;
初识类型、变量和方法
1)初始类型(type)
*数据类型(data type)
2)变量是存放数据的地方,简称 数据
*变量的声明
*变量的使用
3)方法是处理数据的逻辑(成员函数),又称算法
*方法的声明
*方法的调用
4)程序=数据+算法
*有了变量和方法就可以写有意义的程序了
算法简介
1)循环
2)递归
using System;
namespace HanoiTower
{
class Program
{
private static int time = 0;
static void Main(string[] args)
{
int n = Int32.Parse(Console.ReadLine());
Hanoi(n, "TowerA", "TowerB", "TowerC");
Console.WriteLine("=================");
Console.WriteLine(time + " Times");
Console.ReadLine();
}
private static void Hanoi(int n, string origin, string temp, string destination)
{
if (n == 1)
{
move(origin, destination);
time++;
Console.WriteLine("done");
}
else
{
Hanoi(n - 1, origin, destination, temp);
move(origin, destination);
Hanoi(n - 1, temp, origin, destination);
}
}
private static void move(string origin, string destination)
{
Console.WriteLine("Move the plate from " + origin + " to " + destination);
}
}
}