C#基础知识回顾整理

20150907~20150928所学基础知识整理,后续完善补充

数据类型

变量

运算符

语句条件语句循环语句跳转语句异常语句

时间

类、类型

数组、冒泡排序

数组、迷宫操作

集合

结构体

枚举、函数

递归

 

数据类型: 

变量:用于临时存储数据的

变量的定义:

          数据类型 变量名;

          数据类型  变量名=赋值;

          数据类型  变量名1,变量名2,变量名3...;

 

  //生成随机数的类

            Random r = new Random();

            int shu = r.Next(100);

            Console.WriteLine("您的名字打分数为:"+shu);

变量的赋值:变量名=值;

数据类型转换:

             显示转换:直接在数据或变量前,加(类型)

             数值类型跟数值类型之间。

             隐式转换:

                      类型.Parse(数据);

                      Convert.To类型(数据);

             从字符串转成值类型,必须要在数值类型的承受范围之内。

 

运算符

  算数运算符: + - * / % ++ --

  比较运算符:﹥  ﹤  ≧  ≦  == !=

  逻辑运算符:&&  ∣∣ !

优先级:

  前++,前--

  *  /  %

  +  -

  >  <  >=  <=  ==  !=

  &&   ∣∣

  !

有括号的先算括号

 

语句

条件语句:实现选择分支

if (条件表达式)

{                       

               ...

}

循环语句:实现重复操作

跳转语句:结束循环

异常语句:抓取错误

例题 1、输入三个整数XYZ,将他们排序后以从小到大的顺序打印出来。

例题  2、有一组函数:y = x(x < 1); y = 2x - 1(1<=x<10);y = 3x-11(x>10). 括号内是X的满足条件。 实现功能,随意输入一个X值,输出Y的值。

例题  3、输入整数a和b,若a2+b2大于100,则输出a2+b2百位以上数字,否则输出两数之和。

题目:判断某一天是这一年的第多少天

题目:判断某一天是这一年的第多少天

  static void Main(string[] args)
        {
            while (true)//循环
            {
                Console.Write("输入年:");
                int year = int.Parse(Console.ReadLine());
                Console.Write("输入月:");
                int month = int.Parse(Console.ReadLine());
                Console.Write("输入日:");
                int day = int.Parse(Console.ReadLine());

                bool isok = false;//用来记录日期是否正确
                bool isrun = false;
                if (year >= 1759 && year < 9999)
                {
                    if (month >= 1 && month <= 12)
                    {
                        if (month == 2)
                        {
                            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))

//判断闰年能被400整除或者能被4整除同时不能被100整除
                            {
                                if (day >= 1 && day <= 29)
                                {
                                    isok = true;
                                    isrun = true;
                                }
                                else
                                {
                                    Console.WriteLine("应在29天范围之内");
                                }
                            }
                            else
                            {
                                if (day >= 1 && day <= 28)
                                {
                                    isok = true;
                                }
                                else
                                {
                                    Console.WriteLine("应在28天范围之内");
                                }
                            }
                        }
                        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                        {
                            if (day >= 1 && day <= 31)
                            {
                                isok = true;
                            }
                            else
                            {
                                Console.WriteLine("日期应在31天之内");
                            }
                        }

                        if (month == 4 || month == 6 || month == 9 || month == 11)
                        {
                            if (day >= 1 && day <= 30)
                            {
                                isok = true;
                            }
                            else
                            {
                                Console.WriteLine("日期应在30天之内");
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("月份不正确");
                    }
                }
                else
                {
                    Console.WriteLine("年份不正确");
                }

                int sumday = 0;//第多少天
                if (isok)//日期正确,计算天数
                {
                    if (isrun)//是闰年
                    {
                        if (month == 1)
                        {
                            sumday = day;
                        }
                        if (month == 2)
                        {
                            sumday = 31 + day;
                        }
                        if (month == 3)
                        {
                            sumday = 31 + 29 + day;
                        }
                        if (month == 4)
                        {
                            sumday = 31 + 29 + 31 + day;
                        }
                        if (month == 5)
                        {
                            sumday = 31 + 29 + 31 + 30 + day;
                        }
                        if (month == 6)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + day;
                        }
                        if (month == 7)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + 30 + day;
                        }
                        if (month == 8)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day;
                        }
                        if (month == 9)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day;
                        }
                        if (month == 10)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
                        }
                        if (month == 11)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
                        }
                        if (month == 12)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
                        }
                    }
                    else
                    {
                        if (month == 1)
                        {
                            sumday = day;
                        }
                        if (month == 2)
                        {
                            sumday = 31 + day;
                        }
                        if (month == 3)
                        {
                            sumday = 31 + 28 + day;
                        }
                        if (month == 4)
                        {
                            sumday = 31 + 28 + 31 + day;
                        }
                        if (month == 5)
                        {
                            sumday = 31 + 28 + 31 + 30 + day;
                        }
                        if (month == 6)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + day;
                        }
                        if (month == 7)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + 30 + day;
                        }
                        if (month == 8)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
                        }
                        if (month == 9)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
                        }
                        if (month == 10)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
                        }
                        if (month == 11)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
                        }
                        if (month == 12)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
                        }
                    }
                    Console.WriteLine("第{0}天", sumday);
                }
                else
                {
                    Console.WriteLine("已经日期不正确,算不出来!");
                }
                Console.ReadLine();
            }
        }
View Code

 练习题

 ==猜拳题目==

            //猜拳,剪刀-0 石头-1 布-2
            Random r = new Random();//生成随机数
            int diannao = r.Next(3);//生成0-3之间,不包括3的数 

            Console.Write("请出拳:剪刀-0 石头-1 布-2   :");

            int ren = int.Parse(Console.ReadLine());//控制台录入字符串,转换为int类型

            Console.WriteLine("电脑出拳:{0}",diannao);
            if (diannao - ren == -1 || ren - diannao == -2)//所有人赢的情况
            {
                Console.WriteLine("恭喜你,你赢了");
            }
            else if (diannao == ren)
            {
                Console.WriteLine("平局");
            }
            else
            {
                Console.WriteLine("输了");
            }

 == 猜拳:三局两胜===

           int renying = 0;
            int dnying = 0;
            for (int i = 1; i <= 3; i++)
            {
                Random r = new Random();//生成随机数
                int diannao = r.Next(3);//生成0-3之间,不包括3的数

                Console.Write("请出拳:剪刀-0 石头-1 布-2   :");
                int ren = int.Parse(Console.ReadLine());//控制台录入字符串,转换为int类型
                Console.WriteLine("电脑出拳:{0}", diannao);
               if (diannao - ren == -1 || ren - diannao == -2)//所有人赢的情况
                {
                    renying++;
                    Console.WriteLine("恭喜你,你赢了");
                }
                else if (diannao == ren)
                {
                    Console.WriteLine("平局");
                }
                else
                {
                    dnying++;
                    Console.WriteLine("输了");
                }
            }
            if (renying >= 2)
            {
                Console.WriteLine("三局两胜人赢了");
            }
            if (dnying >= 2)
            {
                Console.WriteLine("三局两胜电脑赢了");
            }
 
 ==累加求和==

            int sum = 0;
            Console.Write("请输入数字:");
            int shu = int.Parse(Console.ReadLine());
            for (int i = 1; i <= shu; i++)
            {
                sum = sum + i;=====èsum+ = i ;
            }
            Console.WriteLine("结果为:"+sum);

==输出偶数==

            Console.Write("请输入数字:");
            int shu = int.Parse(Console.ReadLine());
            for (int i = 1; i <= shu; i++)
            {
                if (i % 2 == 0)
                {
                    Console.WriteLine(i);
                }
            }

   ==与7相关的数==

            for (int i = 1; i <= 100; i++)
            {
                if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
                {
                    Console.WriteLine(i);
                }
            }

  ==求阶乘==

            int sum = 1;
            Console.Write("请输入数字:");
            int shu = int.Parse(Console.ReadLine());
            for (int i = 1; i <= shu; i++)
            {
                sum = sum * i ;
            }
            Console.WriteLine("结果为:" + sum);
          
            int sum = 0;
            Console.Write("请输入数字:");
            int shu = int.Parse(Console.ReadLine());
            for (int i = 1; i <= shu; i++)//从1开始遍历
            {
                int jiecheng = 1;
                //求i的阶乘
                for (int j = 1; j <= i; j++)
                {
                    jiecheng = jiecheng*j;
                }
                sum = sum + jiecheng;//累加
            }
            Console.WriteLine("结果为:" + sum);

===篮球====

            Console.Write("请输入次数:");
            int n = int.Parse(Console.ReadLine());
            decimal height = 10M;
            for (int i = 1; i <= n; i++)
            {
                height = height * 3 / 4;
            }
            Console.WriteLine("高度为:"+height);
 
   穷举

======100块钱,买2元一只的圆珠笔,3块钱一个的尺子,5元一个的铅笔盒,每样至少一个,正好花光,有多少种花法。======

            Console.WriteLine("圆珠笔 尺子 铅笔盒");
            int count = 0;
            for (int i = 1; i <= 50; i++)
            {
                for (int j = 1; j <= 33; j++)
                {
                    for (int k = 1; k <= 20; k++)
                    {
                        if (i * 2 + j * 3 + k * 5 == 100)
                        {
                            Console.WriteLine("圆珠笔:{0},尺子{1},铅笔盒{2}",i,j,k);
                            count++;
                        }
                    }
                }
            }
            Console.WriteLine("总个数为:" + count);

====一张纸0.00007m,折多少次和珠峰一样高====

            decimal hou = 0.00007M;
            for (int i = 1; i > 0; i++)
            {
                hou = hou * 2;
                if (hou > 8848)
                {
                    Console.WriteLine(i);
                    break;//跳出循环
                }
            }

====100以内质数====

            for (int k = 1; k <= 100; k++)
            {
                int count = 0;
                for (int i = 1; i <= k; i++)
                {
                    if (k % i == 0)
                    {
                        count++;
                    }
                }
                if (count == 2)
                {
                    Console.WriteLine(k);
                }
            }

  ====100以内质数和====

            int sum = 0;
            for (int k = 1; k <= 100; k++)
            {
                int count = 0;
                for (int i = 1; i <= k; i++)
                {
                    if (k % i == 0)
                    {
                        count++;
                    }
                }
                if (count == 2)
                {
                    sum += k;
                }
            }
            Console.WriteLine(sum);

======一对幼兔,1个月后长成小兔,再过一个月长成成兔并且生下一对幼兔,问,24个月后,有多少对兔子。======

            Console.Write("请输入月数:");
            int m =int.Parse(Console.ReadLine());
            int ct = 0 ;//成兔
            int xt = 0 ;//小兔
            int yt = 1 ;//幼兔
            int zt = 1 ;//总兔
            for (int i = 1; i <= m; i++)
            {
                if (i == 1)
                {
                    ct = 0;
                    xt = 0;
                    yt = 1;
                }
                else
                {
                    ct = xt + ct;
                    xt = yt;
                    yt = ct;
                }
                zt = ct + xt + yt;
                Console.WriteLine(i.ToString() + "个月后兔子总对数是:" + zt.ToString());
                Console.ReadLine();
        }
练习题汇总

循环语句:

while循环:
    初始条件;
    while(条件表达式)
    {
          循环体;
          状态改变;
     }

例:10以内累加求和
int sum =0;
int i = 1;
while(i<=10)
{
   sum+=i;
   i++;
}

do {      }   while();循环

   初始条件;
   do
   {
    循环体;
    状态改变;
    }
    while(条件表达式)
foreach(in)  xx数组集合的
View Code

跳转语句:

Break 结束跳转

Continue 结束本次,继续下次循环

例:
for (int i = 0;i<=10;i++)
{
Console.WriteLine(i);
if (i==3) ________> 0 1 2 3 4 5 6 7 8 9 10
Break;
}


for (int i = 0;i<=10;i++)
{
if (i==3) ________> 0 1 2 4 5 6 7 8 9 10
Break;
Console.WriteLine(i);
}


Random r = new Random();
int a, b, c;
for (int i = 1;i>=1 ;i++ )
{
a = r.Next(10);
b = r.Next(10);
c = r.Next(10);
if(a != b && a != c && b != c)
{
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
break;
}
}
Console.ReadLine();
View Code

异常语句:

 try
{
     int a = int.Parse(Console.ReadLine());
}
catch (Exception ex)
{
     // throw ex;====>抛出异常
    Console.WriteLine("出现错误了:"+ex.Message);
}
finally
{
     //不管
}

 

时间:

//必须通过new初始化变量,后面括号实际是构造函数
//datetime实际是一个类 class

  DateTime dt = new DateTime(2015,9,14);
  Console.WriteLine(dt.ToString());
  Console.ReadLine();

 

例:

int cuo = 0;
Console.WriteLine("请输入:");
string s = Console.ReadLine();
try
{
    DateTime dt = DateTime.Parse(s);
}
catch (Exception ex)
{
     Console.WriteLine("不是" );
     cuo = 1;
}
if( cuo == 0)
{
     Console.WriteLine("正确");
}
Console.ReadLine();
View Code

 

类、类型:

 class类型:用户自定义类型、

            String :处理字符串
                s.Length;//返回字符串的长度
                s.Trim();//去除空格
                s.TrimStart();//去除前空格
                s.TrimEnd();//去除后空格
                s.ToLower();//转换为小写
                s.ToUpper();//转换为大写
  //截取字符串:字符串是有索引的,索引从0开始
           // s.Substring();

//查找索引indexOf  lastindexof replace split

            int id = js.IndexOf("ef",5);//查第一个匹配项的索引
|========>查找第二个索引
            Console.WriteLine(id);
            int lid = js.LastIndexOf("ef");//查找最后一个索引
            Console.WriteLine(lid);

            js.Replace("ef","123");//查找替换
            string news = js.Replace("ef","123");
            Console.WriteLine(js);
            Console.WriteLine(news);

            string s = "a|b|c|ab|cd|c|d";
            string [] str = s.Split('|');//分割
            foreach(string d in str)
            {
                Console.WriteLine(d);
            }
            Datetime:处理时间
            Random:生成随机数
            Math:处理数字的

 //Math:里面有些处理数字的方法,静态方法、
            int i = Math.Abs(-5);//取绝对值
            Console.WriteLine(i);
            double a = Math.Ceiling(1.1);//天花板   上限取整
            Console.WriteLine(a);
            Math.Floor(1.9);//下限取整
            Console.WriteLine(Math.PI);//圆周率
            Math.Round(1.45);//四舍五入
            Console.WriteLine(Math.Round(1.45));
            Console.WriteLine(Math.Pow(2,3));//幂次方
            Console.WriteLine(Math.Sqrt(16));//平方

//Random 随机数
            Random r = new Random();
            int shu = r.Next(1,34);
View Code

 

随机数例题:

 Random r = new Random();
 for (int i= 0;i<=100 ; i++)
 {
      int shu = r.Next(1001,9999);
      Console.WriteLine(shu );
      Thread.Sleep(100);
      Console.Clear(); 
 }
  Console.WriteLine("中奖号码是:");
  Console.ReadLine();
View Code

 

数组、冒泡排序 及相关习题彩票等

static void Main(string[] args)
        { 
            while (true)
            {
            数组:一组同类型的数据,数组是有长度的,数组是有索引的,索引从0开始

=====彩票=====
                int[] shuzu = new int[7];//定义了一个长度为6的int类型的数组
                Random r = new Random();
                for (int i = 0; i < 6; i++)//循环生成六个数
                {
                    shuzu[i] = r.Next(1, 34);//生成一个数
                    bool isok = false;
                    for (int j = 0; j < i; j++)//比较是否跟之前的书相等
                    {
                        if (shuzu[j] == shuzu[i])
                        {
                            isok = true;
                        }
                    }
                    if (isok)
                    {
                        i--;//后退一步
                        continue;
                    }
                }
                shuzu[6] = r.Next(1, 17);
                //输入你的号码
                Console.Write("请输入红球6个,蓝球1个,逗号隔开:");
                string shuru = Console.ReadLine();
                string[] ren = shuru.Split(',');
                //判断中了几个红球
                int count = 0;
                for (int i = 0; i < 6; i++)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        if (int.Parse(ren[i]) == shuzu[j])
                        {
                            count++;
                        }
                    }
                }
                //判断蓝球中没中
                bool islan = false;
                if (int.Parse(ren[6]) == shuzu[6])
                {
                    islan = true;
                }
                //输出电脑随机的中奖号码
                foreach (int a in shuzu)
                {
                    Console.Write(a + " | ");
                }
                //判断中几等奖
                if (count == 6 && islan)
                {
                    Console.Write("一等奖");
                }
                else if (count == 6 && !islan)
                {
                    Console.Write("二等奖");
                }
                else if (count == 5 && islan)
                {
                    Console.Write("三等奖");
                }
                else if ((count == 4 && islan) || (count == 5 && !islan))
                {
                    Console.Write("四等奖");
                }
                else if ((count == 3 && islan) || (count == 4 && !islan))
                {
                    Console.Write("五等奖");
                }
                else if ((count == 2 && islan) || (count == 1 && islan) || (count == 0 && islan))
                {
                    Console.Write("五块钱");
                }
                else
                {
                    Console.Write("别再买了");
                }      
                Console.ReadLine();
           }


====输入10个人的分数,求最高分最低分平均分====

            int[] fenshu = new int[10];
            int max = 0;
            int min = 0;
            int sum = 0;
            for (int i = 0; i < 10; i++)
            {
                fenshu[i] = int.Parse(Console.ReadLine());
                sum += fenshu[i];
                if (i == 0)
                {
                    max = fenshu[i];
                    min = fenshu[i];
                }
                else
                {
                    if (max < fenshu[i])
                    {
                        max = fenshu[i];
                    }
                    if (min > fenshu[i])
                    {
                        min = fenshu[i];
                    }
                }
            }            
            Console.WriteLine("最大值:"+max);
            Console.WriteLine("最小值:" + min);
            Console.WriteLine("平均分:" + sum/10);

====输入全班同学的年龄,按年龄从大到小排序=====

            Console.Write("请输入人数:");
            int n = int.Parse(Console.ReadLine());
            int[] nianling = new int[n];
            for (int i = 0; i < n; i++)
            {
                Console.Write("请输入第{0}个人的年龄:",i+1);
                nianling[i] = int.Parse(Console.ReadLine());
            }
            //开始排序
            for (int j = 0; j < n - 1; j++)
            {
                for (int i = j+1; i < n; i++)
                {
                    if (nianling[i] > nianling[j])
                    {
                        //等量代换
                        int zhong = nianling[i];
                        nianling[i] = nianling[j];
                        nianling[j] = zhong;
                    }
                }
            } 
            foreach (int i in nianling)
            {
                Console.WriteLine(i);
            }
                Console.ReadLine();
        }
View Code

 

数组,简单的迷宫操作

数组:有固定长度的同种类型的一组变量,有索引,索引从0开始。
Int [ ]shuzu = new int[5];
shuzu [0] = 4;
shuzu [1] = 6;
或直接赋值:
int[]shuzu = new int[5]{2,4,5,7,9};
Console.Write(shuzu[3]);
Console.Readline();
这是一维数组,
二维数组是:
int[,] erwei = new int[2,5];   =========>两个长度为5的一维数组
赋值:
Int [ , ] erwei = new int[2,5];
{
    {1,2,3,4,5},
    {2,12,5,7,9}
};
Console.Write(erwei[1,1]);
Console.Readline();
然后练习了简单的迷宫游戏操作。

"\n" ======>换行

"\t" ======>空格
View Code

 

 

 

集合ArrayList:跟数组比,不限数量,不限类型    

集合ArrayList:跟数组比,不限数量,不限类型       
            ArrayList arr = new ArrayList();
            arr.Add(5);
            arr.Add(7);
            arr.Add("fadf");
            string s = "你好";
            arr.Add(s);
            Console.WriteLine(arr[0]);
            
            int count = arr.Count;
            Console.WriteLine("元素个数为:" + count);
           // arr.Clear();//清空集合
            bool isok = arr.Contains(7);
            Console.WriteLine(isok);

            arr.Insert(2,"zhangsan");往集合里插入元素,往指定索引上插入数据
            arr.Remove("zhangsan");移除第一个匹配项
           /arr.RemoveAt(2);移除指定索引上的数据

           int index =  arr.IndexOf("fadf");查找匹配项,返回匹配项的索引
           Console.WriteLine("索引为:"+index);
            foreach (object o in arr)
            {
                Console.WriteLine(o);
            }
            //输入十个人的分数,放入集合当中
            ArrayList list = new ArrayList();
            for (int i = 0; i < 5; i++)
            {
                string s = Console.ReadLine();
                list.Add(s);
            }
            list.Sort();//排序
            list.Reverse();//翻转集合
            foreach (string s in list)
            {
                Console.WriteLine(s);
            } 

  特殊集合stack、 queue、 hashtable

            //栈桥
            Stack s = new Stack();
            s.Push(3);//推入集合
            s.Push(5);
            s.Push(7);
            foreach(int a in s)
            {
                Console.WriteLine(a);
            }
            int count = s.Count;
            int qu = int.Parse(s.Pop().ToString());//弹出最后一个元素
            Console.WriteLine(qu);
            s.Clear();//清空集合
            //队列
            Queue q = new Queue();
            q.Enqueue(3);
            q.Enqueue(5);
            q.Enqueue(7);
            int chu = int.Parse(q.Dequeue().ToString());
            foreach (int a in q)
            {
                Console.WriteLine(a);
            }
            Hashtable hs = new Hashtable();
            hs.Add(3, "张三");
            hs.Add("4", "李四");
            //foreach (int i in hs.Keys)
            //{
            //    Console.WriteLine(i);
            //}
            foreach (string s in hs.Values)
            {
                Console.WriteLine(s);
            }
            int count = hs.Count;//元素个数
            Console.WriteLine(hs["4"]);
            Console.ReadLine();
View Code

 

 结构体及题目

结构体:用户自定义数据类型,实际就是变量组,可以一次存多个不同变量
    结构体定义在main函数的外面
Struck 结构体名
{
       // 元素一
       // 元素二
}
题目:定义一个学生的结构体,学号,姓名,身高,输入学生信息,按身高排序输出

枚举、函数:

枚举:常量组

常量:变量前面加const

 

枚举的索引:

 

函数:

修饰符  返回值  函数名(形参1,形参2,……形参N)

{

                     函数体

}

当一个流程完全封闭,完整的时候,并且需要多次使用的时候才去写函数。

写在main函数外面,class里面

无返回值无参数

有返回值,无参数的

有返回值,有参数的

有多个返回值

 

猜拳、输出偶数:

 

函数返回多个值:一元二次方程

练习题目:一元二次方程

 

递归:卖羊,没过一个村庄卖掉总数的二分之一零一只羊,过了七个村庄后还剩两只,问最初赶了多少只羊

 

---恢复内容结束---

数据类型

变量

运算符

语句条件语句循环语句跳转语句异常语句

时间

类、类型

数组、冒泡排序

数组、迷宫操作

集合

结构体

枚举、函数

递归

 

数据类型: 

变量:用于临时存储数据的

变量的定义:

          数据类型 变量名;

          数据类型  变量名=赋值;

          数据类型  变量名1,变量名2,变量名3...;

 

  //生成随机数的类

            Random r = new Random();

            int shu = r.Next(100);

            Console.WriteLine("您的名字打分数为:"+shu);

变量的赋值:变量名=值;

数据类型转换:

             显示转换:直接在数据或变量前,加(类型)

             数值类型跟数值类型之间。

             隐式转换:

                      类型.Parse(数据);

                      Convert.To类型(数据);

             从字符串转成值类型,必须要在数值类型的承受范围之内。

 

运算符

  算数运算符: + - * / % ++ --

  比较运算符:﹥  ﹤  ≧  ≦  == !=

  逻辑运算符:&&  ∣∣ !

优先级:

  前++,前--

  *  /  %

  +  -

  >  <  >=  <=  ==  !=

  &&   ∣∣

  !

有括号的先算括号

 

语句

条件语句:实现选择分支

if (条件表达式)

{                       

               ...

}

循环语句:实现重复操作

跳转语句:结束循环

异常语句:抓取错误

例题 1、输入三个整数XYZ,将他们排序后以从小到大的顺序打印出来。

例题  2、有一组函数:y = x(x < 1); y = 2x - 1(1<=x<10);y = 3x-11(x>10). 括号内是X的满足条件。 实现功能,随意输入一个X值,输出Y的值。

例题  3、输入整数a和b,若a2+b2大于100,则输出a2+b2百位以上数字,否则输出两数之和。

题目:判断某一天是这一年的第多少天

题目:判断某一天是这一年的第多少天

  static void Main(string[] args)
        {
            while (true)//循环
            {
                Console.Write("输入年:");
                int year = int.Parse(Console.ReadLine());
                Console.Write("输入月:");
                int month = int.Parse(Console.ReadLine());
                Console.Write("输入日:");
                int day = int.Parse(Console.ReadLine());

                bool isok = false;//用来记录日期是否正确
                bool isrun = false;
                if (year >= 1759 && year < 9999)
                {
                    if (month >= 1 && month <= 12)
                    {
                        if (month == 2)
                        {
                            if (year % 400 == 0 || (year % 4 == 0 && year % 100 != 0))

//判断闰年能被400整除或者能被4整除同时不能被100整除
                            {
                                if (day >= 1 && day <= 29)
                                {
                                    isok = true;
                                    isrun = true;
                                }
                                else
                                {
                                    Console.WriteLine("应在29天范围之内");
                                }
                            }
                            else
                            {
                                if (day >= 1 && day <= 28)
                                {
                                    isok = true;
                                }
                                else
                                {
                                    Console.WriteLine("应在28天范围之内");
                                }
                            }
                        }
                        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                        {
                            if (day >= 1 && day <= 31)
                            {
                                isok = true;
                            }
                            else
                            {
                                Console.WriteLine("日期应在31天之内");
                            }
                        }

                        if (month == 4 || month == 6 || month == 9 || month == 11)
                        {
                            if (day >= 1 && day <= 30)
                            {
                                isok = true;
                            }
                            else
                            {
                                Console.WriteLine("日期应在30天之内");
                            }
                        }
                    }
                    else
                    {
                        Console.WriteLine("月份不正确");
                    }
                }
                else
                {
                    Console.WriteLine("年份不正确");
                }

                int sumday = 0;//第多少天
                if (isok)//日期正确,计算天数
                {
                    if (isrun)//是闰年
                    {
                        if (month == 1)
                        {
                            sumday = day;
                        }
                        if (month == 2)
                        {
                            sumday = 31 + day;
                        }
                        if (month == 3)
                        {
                            sumday = 31 + 29 + day;
                        }
                        if (month == 4)
                        {
                            sumday = 31 + 29 + 31 + day;
                        }
                        if (month == 5)
                        {
                            sumday = 31 + 29 + 31 + 30 + day;
                        }
                        if (month == 6)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + day;
                        }
                        if (month == 7)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + 30 + day;
                        }
                        if (month == 8)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + day;
                        }
                        if (month == 9)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + day;
                        }
                        if (month == 10)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
                        }
                        if (month == 11)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
                        }
                        if (month == 12)
                        {
                            sumday = 31 + 29 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
                        }
                    }
                    else
                    {
                        if (month == 1)
                        {
                            sumday = day;
                        }
                        if (month == 2)
                        {
                            sumday = 31 + day;
                        }
                        if (month == 3)
                        {
                            sumday = 31 + 28 + day;
                        }
                        if (month == 4)
                        {
                            sumday = 31 + 28 + 31 + day;
                        }
                        if (month == 5)
                        {
                            sumday = 31 + 28 + 31 + 30 + day;
                        }
                        if (month == 6)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + day;
                        }
                        if (month == 7)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + 30 + day;
                        }
                        if (month == 8)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + day;
                        }
                        if (month == 9)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + day;
                        }
                        if (month == 10)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
                        }
                        if (month == 11)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + day;
                        }
                        if (month == 12)
                        {
                            sumday = 31 + 28 + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31 + 30 + day;
                        }
                    }
                    Console.WriteLine("第{0}天", sumday);
                }
                else
                {
                    Console.WriteLine("已经日期不正确,算不出来!");
                }
                Console.ReadLine();
            }
        }
View Code

 练习题

 ==猜拳题目==

            //猜拳,剪刀-0 石头-1 布-2
            Random r = new Random();//生成随机数
            int diannao = r.Next(3);//生成0-3之间,不包括3的数 

            Console.Write("请出拳:剪刀-0 石头-1 布-2   :");

            int ren = int.Parse(Console.ReadLine());//控制台录入字符串,转换为int类型

            Console.WriteLine("电脑出拳:{0}",diannao);
            if (diannao - ren == -1 || ren - diannao == -2)//所有人赢的情况
            {
                Console.WriteLine("恭喜你,你赢了");
            }
            else if (diannao == ren)
            {
                Console.WriteLine("平局");
            }
            else
            {
                Console.WriteLine("输了");
            }

 == 猜拳:三局两胜===

           int renying = 0;
            int dnying = 0;
            for (int i = 1; i <= 3; i++)
            {
                Random r = new Random();//生成随机数
                int diannao = r.Next(3);//生成0-3之间,不包括3的数

                Console.Write("请出拳:剪刀-0 石头-1 布-2   :");
                int ren = int.Parse(Console.ReadLine());//控制台录入字符串,转换为int类型
                Console.WriteLine("电脑出拳:{0}", diannao);
               if (diannao - ren == -1 || ren - diannao == -2)//所有人赢的情况
                {
                    renying++;
                    Console.WriteLine("恭喜你,你赢了");
                }
                else if (diannao == ren)
                {
                    Console.WriteLine("平局");
                }
                else
                {
                    dnying++;
                    Console.WriteLine("输了");
                }
            }
            if (renying >= 2)
            {
                Console.WriteLine("三局两胜人赢了");
            }
            if (dnying >= 2)
            {
                Console.WriteLine("三局两胜电脑赢了");
            }
 
 ==累加求和==

            int sum = 0;
            Console.Write("请输入数字:");
            int shu = int.Parse(Console.ReadLine());
            for (int i = 1; i <= shu; i++)
            {
                sum = sum + i;=====èsum+ = i ;
            }
            Console.WriteLine("结果为:"+sum);

==输出偶数==

            Console.Write("请输入数字:");
            int shu = int.Parse(Console.ReadLine());
            for (int i = 1; i <= shu; i++)
            {
                if (i % 2 == 0)
                {
                    Console.WriteLine(i);
                }
            }

   ==与7相关的数==

            for (int i = 1; i <= 100; i++)
            {
                if (i % 7 == 0 || i % 10 == 7 || i / 10 == 7)
                {
                    Console.WriteLine(i);
                }
            }

  ==求阶乘==

            int sum = 1;
            Console.Write("请输入数字:");
            int shu = int.Parse(Console.ReadLine());
            for (int i = 1; i <= shu; i++)
            {
                sum = sum * i ;
            }
            Console.WriteLine("结果为:" + sum);
          
            int sum = 0;
            Console.Write("请输入数字:");
            int shu = int.Parse(Console.ReadLine());
            for (int i = 1; i <= shu; i++)//从1开始遍历
            {
                int jiecheng = 1;
                //求i的阶乘
                for (int j = 1; j <= i; j++)
                {
                    jiecheng = jiecheng*j;
                }
                sum = sum + jiecheng;//累加
            }
            Console.WriteLine("结果为:" + sum);

===篮球====

            Console.Write("请输入次数:");
            int n = int.Parse(Console.ReadLine());
            decimal height = 10M;
            for (int i = 1; i <= n; i++)
            {
                height = height * 3 / 4;
            }
            Console.WriteLine("高度为:"+height);
 
   穷举

======100块钱,买2元一只的圆珠笔,3块钱一个的尺子,5元一个的铅笔盒,每样至少一个,正好花光,有多少种花法。======

            Console.WriteLine("圆珠笔 尺子 铅笔盒");
            int count = 0;
            for (int i = 1; i <= 50; i++)
            {
                for (int j = 1; j <= 33; j++)
                {
                    for (int k = 1; k <= 20; k++)
                    {
                        if (i * 2 + j * 3 + k * 5 == 100)
                        {
                            Console.WriteLine("圆珠笔:{0},尺子{1},铅笔盒{2}",i,j,k);
                            count++;
                        }
                    }
                }
            }
            Console.WriteLine("总个数为:" + count);

====一张纸0.00007m,折多少次和珠峰一样高====

            decimal hou = 0.00007M;
            for (int i = 1; i > 0; i++)
            {
                hou = hou * 2;
                if (hou > 8848)
                {
                    Console.WriteLine(i);
                    break;//跳出循环
                }
            }

====100以内质数====

            for (int k = 1; k <= 100; k++)
            {
                int count = 0;
                for (int i = 1; i <= k; i++)
                {
                    if (k % i == 0)
                    {
                        count++;
                    }
                }
                if (count == 2)
                {
                    Console.WriteLine(k);
                }
            }

  ====100以内质数和====

            int sum = 0;
            for (int k = 1; k <= 100; k++)
            {
                int count = 0;
                for (int i = 1; i <= k; i++)
                {
                    if (k % i == 0)
                    {
                        count++;
                    }
                }
                if (count == 2)
                {
                    sum += k;
                }
            }
            Console.WriteLine(sum);

======一对幼兔,1个月后长成小兔,再过一个月长成成兔并且生下一对幼兔,问,24个月后,有多少对兔子。======

            Console.Write("请输入月数:");
            int m =int.Parse(Console.ReadLine());
            int ct = 0 ;//成兔
            int xt = 0 ;//小兔
            int yt = 1 ;//幼兔
            int zt = 1 ;//总兔
            for (int i = 1; i <= m; i++)
            {
                if (i == 1)
                {
                    ct = 0;
                    xt = 0;
                    yt = 1;
                }
                else
                {
                    ct = xt + ct;
                    xt = yt;
                    yt = ct;
                }
                zt = ct + xt + yt;
                Console.WriteLine(i.ToString() + "个月后兔子总对数是:" + zt.ToString());
                Console.ReadLine();
        }
练习题汇总

循环语句:

while循环:
    初始条件;
    while(条件表达式)
    {
          循环体;
          状态改变;
     }

例:10以内累加求和
int sum =0;
int i = 1;
while(i<=10)
{
   sum+=i;
   i++;
}

do {      }   while();循环

   初始条件;
   do
   {
    循环体;
    状态改变;
    }
    while(条件表达式)
foreach(in)  xx数组集合的
View Code

跳转语句:

Break 结束跳转

Continue 结束本次,继续下次循环

例:
for (int i = 0;i<=10;i++)
{
Console.WriteLine(i);
if (i==3) ________> 0 1 2 3 4 5 6 7 8 9 10
Break;
}


for (int i = 0;i<=10;i++)
{
if (i==3) ________> 0 1 2 4 5 6 7 8 9 10
Break;
Console.WriteLine(i);
}


Random r = new Random();
int a, b, c;
for (int i = 1;i>=1 ;i++ )
{
a = r.Next(10);
b = r.Next(10);
c = r.Next(10);
if(a != b && a != c && b != c)
{
Console.WriteLine(a);
Console.WriteLine(b);
Console.WriteLine(c);
break;
}
}
Console.ReadLine();
View Code

异常语句:

 try
{
     int a = int.Parse(Console.ReadLine());
}
catch (Exception ex)
{
     // throw ex;====>抛出异常
    Console.WriteLine("出现错误了:"+ex.Message);
}
finally
{
     //不管
}

 

时间:

//必须通过new初始化变量,后面括号实际是构造函数
//datetime实际是一个类 class

  DateTime dt = new DateTime(2015,9,14);
  Console.WriteLine(dt.ToString());
  Console.ReadLine();

 

例:

int cuo = 0;
Console.WriteLine("请输入:");
string s = Console.ReadLine();
try
{
    DateTime dt = DateTime.Parse(s);
}
catch (Exception ex)
{
     Console.WriteLine("不是" );
     cuo = 1;
}
if( cuo == 0)
{
     Console.WriteLine("正确");
}
Console.ReadLine();
View Code

 

类、类型:

 class类型:用户自定义类型、

            String :处理字符串
                s.Length;//返回字符串的长度
                s.Trim();//去除空格
                s.TrimStart();//去除前空格
                s.TrimEnd();//去除后空格
                s.ToLower();//转换为小写
                s.ToUpper();//转换为大写
  //截取字符串:字符串是有索引的,索引从0开始
           // s.Substring();

//查找索引indexOf  lastindexof replace split

            int id = js.IndexOf("ef",5);//查第一个匹配项的索引
|========>查找第二个索引
            Console.WriteLine(id);
            int lid = js.LastIndexOf("ef");//查找最后一个索引
            Console.WriteLine(lid);

            js.Replace("ef","123");//查找替换
            string news = js.Replace("ef","123");
            Console.WriteLine(js);
            Console.WriteLine(news);

            string s = "a|b|c|ab|cd|c|d";
            string [] str = s.Split('|');//分割
            foreach(string d in str)
            {
                Console.WriteLine(d);
            }
            Datetime:处理时间
            Random:生成随机数
            Math:处理数字的

 //Math:里面有些处理数字的方法,静态方法、
            int i = Math.Abs(-5);//取绝对值
            Console.WriteLine(i);
            double a = Math.Ceiling(1.1);//天花板   上限取整
            Console.WriteLine(a);
            Math.Floor(1.9);//下限取整
            Console.WriteLine(Math.PI);//圆周率
            Math.Round(1.45);//四舍五入
            Console.WriteLine(Math.Round(1.45));
            Console.WriteLine(Math.Pow(2,3));//幂次方
            Console.WriteLine(Math.Sqrt(16));//平方

//Random 随机数
            Random r = new Random();
            int shu = r.Next(1,34);
View Code

 

随机数例题:

 Random r = new Random();
 for (int i= 0;i<=100 ; i++)
 {
      int shu = r.Next(1001,9999);
      Console.WriteLine(shu );
      Thread.Sleep(100);
      Console.Clear(); 
 }
  Console.WriteLine("中奖号码是:");
  Console.ReadLine();
View Code

 

数组、冒泡排序 及相关习题彩票等

static void Main(string[] args)
        { 
            while (true)
            {
            数组:一组同类型的数据,数组是有长度的,数组是有索引的,索引从0开始

=====彩票=====
                int[] shuzu = new int[7];//定义了一个长度为6的int类型的数组
                Random r = new Random();
                for (int i = 0; i < 6; i++)//循环生成六个数
                {
                    shuzu[i] = r.Next(1, 34);//生成一个数
                    bool isok = false;
                    for (int j = 0; j < i; j++)//比较是否跟之前的书相等
                    {
                        if (shuzu[j] == shuzu[i])
                        {
                            isok = true;
                        }
                    }
                    if (isok)
                    {
                        i--;//后退一步
                        continue;
                    }
                }
                shuzu[6] = r.Next(1, 17);
                //输入你的号码
                Console.Write("请输入红球6个,蓝球1个,逗号隔开:");
                string shuru = Console.ReadLine();
                string[] ren = shuru.Split(',');
                //判断中了几个红球
                int count = 0;
                for (int i = 0; i < 6; i++)
                {
                    for (int j = 0; j < 6; j++)
                    {
                        if (int.Parse(ren[i]) == shuzu[j])
                        {
                            count++;
                        }
                    }
                }
                //判断蓝球中没中
                bool islan = false;
                if (int.Parse(ren[6]) == shuzu[6])
                {
                    islan = true;
                }
                //输出电脑随机的中奖号码
                foreach (int a in shuzu)
                {
                    Console.Write(a + " | ");
                }
                //判断中几等奖
                if (count == 6 && islan)
                {
                    Console.Write("一等奖");
                }
                else if (count == 6 && !islan)
                {
                    Console.Write("二等奖");
                }
                else if (count == 5 && islan)
                {
                    Console.Write("三等奖");
                }
                else if ((count == 4 && islan) || (count == 5 && !islan))
                {
                    Console.Write("四等奖");
                }
                else if ((count == 3 && islan) || (count == 4 && !islan))
                {
                    Console.Write("五等奖");
                }
                else if ((count == 2 && islan) || (count == 1 && islan) || (count == 0 && islan))
                {
                    Console.Write("五块钱");
                }
                else
                {
                    Console.Write("别再买了");
                }      
                Console.ReadLine();
           }


====输入10个人的分数,求最高分最低分平均分====

            int[] fenshu = new int[10];
            int max = 0;
            int min = 0;
            int sum = 0;
            for (int i = 0; i < 10; i++)
            {
                fenshu[i] = int.Parse(Console.ReadLine());
                sum += fenshu[i];
                if (i == 0)
                {
                    max = fenshu[i];
                    min = fenshu[i];
                }
                else
                {
                    if (max < fenshu[i])
                    {
                        max = fenshu[i];
                    }
                    if (min > fenshu[i])
                    {
                        min = fenshu[i];
                    }
                }
            }            
            Console.WriteLine("最大值:"+max);
            Console.WriteLine("最小值:" + min);
            Console.WriteLine("平均分:" + sum/10);

====输入全班同学的年龄,按年龄从大到小排序=====

            Console.Write("请输入人数:");
            int n = int.Parse(Console.ReadLine());
            int[] nianling = new int[n];
            for (int i = 0; i < n; i++)
            {
                Console.Write("请输入第{0}个人的年龄:",i+1);
                nianling[i] = int.Parse(Console.ReadLine());
            }
            //开始排序
            for (int j = 0; j < n - 1; j++)
            {
                for (int i = j+1; i < n; i++)
                {
                    if (nianling[i] > nianling[j])
                    {
                        //等量代换
                        int zhong = nianling[i];
                        nianling[i] = nianling[j];
                        nianling[j] = zhong;
                    }
                }
            } 
            foreach (int i in nianling)
            {
                Console.WriteLine(i);
            }
                Console.ReadLine();
        }
View Code

 

数组,简单的迷宫操作

数组:有固定长度的同种类型的一组变量,有索引,索引从0开始。
Int [ ]shuzu = new int[5];
shuzu [0] = 4;
shuzu [1] = 6;
或直接赋值:
int[]shuzu = new int[5]{2,4,5,7,9};
Console.Write(shuzu[3]);
Console.Readline();
这是一维数组,
二维数组是:
int[,] erwei = new int[2,5];   =========>两个长度为5的一维数组
赋值:
Int [ , ] erwei = new int[2,5];
{
    {1,2,3,4,5},
    {2,12,5,7,9}
};
Console.Write(erwei[1,1]);
Console.Readline();
然后练习了简单的迷宫游戏操作。

"\n" ======>换行

"\t" ======>空格
View Code

 

 

 

集合ArrayList:跟数组比,不限数量,不限类型    

集合ArrayList:跟数组比,不限数量,不限类型       
            ArrayList arr = new ArrayList();
            arr.Add(5);
            arr.Add(7);
            arr.Add("fadf");
            string s = "你好";
            arr.Add(s);
            Console.WriteLine(arr[0]);
            
            int count = arr.Count;
            Console.WriteLine("元素个数为:" + count);
           // arr.Clear();//清空集合
            bool isok = arr.Contains(7);
            Console.WriteLine(isok);

            arr.Insert(2,"zhangsan");往集合里插入元素,往指定索引上插入数据
            arr.Remove("zhangsan");移除第一个匹配项
           /arr.RemoveAt(2);移除指定索引上的数据

           int index =  arr.IndexOf("fadf");查找匹配项,返回匹配项的索引
           Console.WriteLine("索引为:"+index);
            foreach (object o in arr)
            {
                Console.WriteLine(o);
            }
            //输入十个人的分数,放入集合当中
            ArrayList list = new ArrayList();
            for (int i = 0; i < 5; i++)
            {
                string s = Console.ReadLine();
                list.Add(s);
            }
            list.Sort();//排序
            list.Reverse();//翻转集合
            foreach (string s in list)
            {
                Console.WriteLine(s);
            } 

  特殊集合stack、 queue、 hashtable

            //栈桥
            Stack s = new Stack();
            s.Push(3);//推入集合
            s.Push(5);
            s.Push(7);
            foreach(int a in s)
            {
                Console.WriteLine(a);
            }
            int count = s.Count;
            int qu = int.Parse(s.Pop().ToString());//弹出最后一个元素
            Console.WriteLine(qu);
            s.Clear();//清空集合
            //队列
            Queue q = new Queue();
            q.Enqueue(3);
            q.Enqueue(5);
            q.Enqueue(7);
            int chu = int.Parse(q.Dequeue().ToString());
            foreach (int a in q)
            {
                Console.WriteLine(a);
            }
            Hashtable hs = new Hashtable();
            hs.Add(3, "张三");
            hs.Add("4", "李四");
            //foreach (int i in hs.Keys)
            //{
            //    Console.WriteLine(i);
            //}
            foreach (string s in hs.Values)
            {
                Console.WriteLine(s);
            }
            int count = hs.Count;//元素个数
            Console.WriteLine(hs["4"]);
            Console.ReadLine();
View Code

 

 结构体及题目

结构体:用户自定义数据类型,实际就是变量组,可以一次存多个不同变量
    结构体定义在main函数的外面
Struck 结构体名
{
       // 元素一
       // 元素二
}
题目:定义一个学生的结构体,学号,姓名,身高,输入学生信息,按身高排序输出

枚举、函数:

枚举:常量组

常量:变量前面加const

 

枚举的索引:

 

函数:

修饰符  返回值  函数名(形参1,形参2,……形参N)

{

                     函数体

}

当一个流程完全封闭,完整的时候,并且需要多次使用的时候才去写函数。

写在main函数外面,class里面

无返回值无参数

有返回值,无参数的

有返回值,有参数的

有多个返回值

 

猜拳、输出偶数:

 

函数返回多个值:一元二次方程

练习题目:一元二次方程

 

递归:卖羊,没过一个村庄卖掉总数的二分之一零一只羊,过了七个村庄后还剩两只,问最初赶了多少只羊

 

posted @ 2017-04-10 11:19  m-n  阅读(539)  评论(0编辑  收藏  举报