05c#语言基本元素概览,初始类型,变量与方法,算法简介

在这里插入图片描述

构成c#语言的基本元素

在这里插入图片描述

1.关键字
在这里插入图片描述在这里插入图片描述
2.操作符
在这里插入图片描述
3.标识符
学习阅读c#语言文档
大小写规范
驼峰法:头一个单词首字母小写,其余后面每个单词首字母大写。变量名
构成关键字每个单词首字母大写。方法名,类名,名称空间
4.标点符号
;——语句结束
{}
不参与运算
5.文本(字面值)
整数

int x=3;
long y=3L;

实数

float x=3.0F;//单精度数,32位表示一个浮点数
double y=4.0;//双精度数,64位表是浮点数

默认双精度数,表示单精度数后面必须加F

字符

char c='a';

字符串

string str="a";

布尔

bool b=true;
bool b2=false;

空值

string str=null;
Form f=null;//空引用出错,System.NullReferenceException
f.showdialog();

6.注释和空白
单行注释://string str=null;
块注释:/*string str=null;*/
块注释不能嵌套

ctrl+E,D:格式化代码

初识类型、变量、方法名

类型 Type

var  x=3L;//自动推断出什么类型,变量
Console.WriteLine(x,GetType().Name);//int32,int64

变量

int x;
x=100;
float x;
x=3.0F;
double x;
x=3.0;

方法

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

namespace demo1
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            int x = c.Add(2, 3);
            Console.WriteLine(x);
            Console.ReadLine();
        }
    }
    class Calculator
    {
        public int Add(int a, int b)
        {
            int result = a + b;
            return result;

        }
    }
}

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

namespace demo1
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            string str = c.Today();
            Console.WriteLine(str);
            Console.ReadLine();
        }
    }
    class Calculator
    {
        public int Add(int a, int b)
        {
            int result = a + b;
            return result;

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

        }
    }
}

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

namespace demo1
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            string str = c.Today();
            c.PrintSum(4,6); Console.ReadLine();
        }
    }
    class Calculator
    {
        public int Add(int a, int b)
        {
            int result = a + b;
            return result;

        }
        public string Today()
        {
            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;

namespace demo1
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            c.PrintXTo1(10);
            Console.ReadLine();
        }
    }
    class Calculator
    {
        public void PrintXTo1(int x)
        {
            for (int i = x; i >0; i--)
            {
                Console.WriteLine(i);
            }
        }
    
    }
}

注释
ctrl+kc
取消注释
ctrl+ku

递归

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

namespace demo1
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            c.PrintXTo1(3);
            Console.ReadLine();
        }
    }
    class Calculator
    {
        //public void PrintXTo1(int x)
        //{
        //    for (int i = x; i >0; i--)
        //    {
        //        Console.WriteLine(i);
        //    }
        //}
        public void PrintXTo1(int x)
        {
            if (x == 1)
            {
                Console.WriteLine(x);
            }
            else
            {
                Console.WriteLine(x);
                PrintXTo1(x-1);
            }
            
        }
    }
}

计算从1到100的和

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

namespace demo1
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            int result = c.SumFrom1ToX(100);
            Console.WriteLine(result);
            Console.ReadLine();
        }
    }
    class Calculator
    {
       //public int SumFrom1ToX1(int x)
       // {
       //     int result = 0;
       //     for (int i = 1; i < x+1; i++)
       //     {
       //         result = result + i;
       //     }
       //     return result;
       // }//非递归
        public int SumFrom1ToX2(int x)
        {
       
            if (x==1)
            {
                return 1;
            }
            else
            {
                int result = x+ SumFrom1ToX(x-1);
                return result;
            }
         
        }//递归
         public int SumFrom1ToX3(int x)
        {
            return (1 + x) * x / 2;
        }
    }
}

作业:
在这里插入图片描述
 André Karwath aka Aka - 自己的作品

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

namespace demo1
{
    class Program
    {
        static void Main(string[] args)
        {
            Calculator c = new Calculator();
            int n = 8;
            c.hannoi(n, 'A', 'B', 'C');
          
            Console.ReadLine();
        }
    }
    class Calculator
    {
        public void hannoi(int n, char from, char buffer, char to)
        {
       
            if (n == 0)
                return;
            hannoi(n - 1, from, to, buffer);
            Console.Write("Move disk ");
            Console.Write(n);
            Console.Write(" from ");
            Console.Write(from);
            Console.Write(" to ");
            Console.WriteLine(to);
            hannoi(n - 1, buffer, from, to);
         
        }
    }
}

  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值