代码改变世界

c#复习

2016-07-05 16:11  天疯狂石  阅读(138)  评论(0编辑  收藏  举报

 //语句:顺序语句、分支语句、循环语句
            //分支语句 
            //1.if     若满足条件,就进入,若不满足,跳过
            //int a = 100;
            //if (a < 100)
            //{
            //    Console.WriteLine("您输入的是100以内的数。");
            //}

            //2.if    else   二选一
            //若if满足,else一定不满足
            //反之,else必须走
            //int a = 10;
            //if (a < 10)
            //{
            //    Console.WriteLine("您输入的数小于10的数");
            //}
            //else//另外的其他的     a>=10
            //{
            //    Console.WriteLine("您输入的不是小于10的数。");
            //}

            //3.if    else if     else if    ....     else    多选一
            //只要有一个满足,那从这个满足条件的if  或else if下面的都不用查看
            //若上面的if   或else if都不满足,那么一定要走else
            //int a = 10;
            //if (a < 10)
            //{

            //}
            //else if (a == 10)
            //{

            //}
            //else if (a < 100)
            //{

            //}
            //else//a>=100
            //{

            //}


            //4.if的嵌套
            //在满足一定的大范围之内,再去查看是否满足小的条件
            //请输入分数,产看是否及格,若及格,查看是否80分以及以上,评价A
            //Console.Write("请输入您的分数:");
            //double score = double.Parse(Console.ReadLine());
            //if (score >= 0 && score <= 100)
            //{
            //    if(score>=60)
            //    {
            //        Console.WriteLine("及格。");
            //        if (score >= 80)
            //        {
            //            Console.WriteLine("您的分数评价是A。");
            //        }
            //    }
            //    else//score>=0&&score<60
            //    {
            //        Console.WriteLine("不及格。");
            //    }
            //}
            //else//不在0~100之间
            //{
            //    Console.WriteLine("输入错误!");
            //}


            //switch case     多选一
            //Console.WriteLine("1.汉堡包");
            //Console.WriteLine("2.鸡腿");
            //Console.WriteLine("3.可乐");
            //Console.WriteLine("4.鸡米花");
            //Console.Write("请输入您要的物品编号:");
            //string shu = Console.ReadLine();

            //switch (shu)
            //{
            //    case "1":
            //        Console.WriteLine("您选择的是汉堡包!");
            //        break;//跳出最近的花括号    跳出switch
            //    case "2":
            //        Console.WriteLine("您选择的是汉堡包!");
            //        break;//跳出最近的花括号    跳出switch
            //    case "3":
            //        Console.WriteLine("您选择的是汉堡包!");
            //        break;//跳出最近的花括号    跳出switch
            //    case "4":
            //        Console.WriteLine("您选择的是汉堡包!");
            //        break;//跳出最近的花括号    跳出switch
            //    default://相当于else    上面的值都不满足,就走这一条
            //        Console.WriteLine("输入有误!!");
            //        break;
            //}


            //if (shu == "1")
            //{
            //    Console.WriteLine("您选择的是汉堡包!");
            //}
            //else if (shu == "2")
            //{
            //    Console.WriteLine("您选择的是汉堡包!");
            //}
            //else if (shu == "3")
            //{
            //    Console.WriteLine("您选择的是汉堡包!");
            //}
            //else if (shu == "4")
            //{
            //    Console.WriteLine("您选择的是汉堡包!");
            //}
            //else
            //{
            //    Console.WriteLine("输入错误!");
            //}


            //输入年,月,日   判断时间日期格式是否正确
            Console.Write("请输入年份:");
            int year = int.Parse(Console.ReadLine());
            if (year >= 0 && year <= 9999)
            {
                Console.Write("请输入月份:");
                int month = int.Parse(Console.ReadLine());
                if (month >= 1 && month <= 12)
                {
                    Console.Write("请输入天");
                    int day = int.Parse(Console.ReadLine());
                    if (day >= 1 && day <= 31)
                    {
                        if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                        {
                            Console.WriteLine("您输入的日期正确,日期是:" + year+month+day);
                        }
                        if (day >= 1 && day <= 30)
                        {
                            if (month == 4 || month == 6 || month == 9 || month == 11)
                            {
                                Console.WriteLine("您的输入正确,日期是:" + year+month+day);
                            }
                            if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0)
                            {
                                if (day >= 1 && day <= 29)
                                {
                                    Console.WriteLine("您输入的日期是正确,日期是:" + year+month+day);
                                }
                                else
                                {
                                    Console.WriteLine("您的输入有误");
                                }
                            }
                            else
                            {
                                if (day >= 1 && day <= 28)
                                {
                                    Console.WriteLine("您输出的是平年" + year +month +day);
                                }
                                else
                                {
                                    Console.WriteLine("您的输入有误");
                                }
                            }


                        }

 

                        else
                        {
                            Console.WriteLine("您的输入有误");
                        }
                    }
                    else
                    {
                        Console.WriteLine("您的输入有误");
                    }
                }
                else
                {
                    Console.WriteLine("您的输入有误");
                }
            }
                Console.ReadLine();

//循环:初始条件,循环条件,循环体,状态改变
            //for (int i = 0; i < 5; i++)
            //{
            //    Console.WriteLine("你好");
            //    //循环体
            //}
            //Console.ReadLine();

            //累加求和
            //Console.Write("输入一个正整数:");
            //int a = int.Parse(Console.ReadLine());
            //int sum = 0;
            //for (int i = 1; i <= a; i++)
            //{
            //    sum += i;
            //}
            //Console.WriteLine(sum);
            //Console.ReadLine();


            //死循环
            //for (; ; )
            //{
            //    Console.WriteLine(123);
            //}


            //for循环的嵌套
            //外循环控制行    i
            //内循环控制列    j
            //for (int i = 0; i < 10; i++)
            //{
            //    for (int j = 0; j < 10; j++)
            //    {
            //        Console.Write("☆");
            //    }
            //    Console.WriteLine();
            //}
            //Console.ReadLine();


            //打印一个左下角为直角的三角形
            //for (int i = 0; i < 10; i++)
            //{
            //    for (int j = 0; j < i+1; j++)
            //    {
            //        Console.Write("☆");
            //    }
            //    Console.WriteLine();
            //}
            //Console.ReadLine();


            //打印一个右下角为直角的三角形
            //for (int i = 0; i < 10; i++)
            //{
            //    for (int k = 0; k < 9 - i; k++)
            //    {
            //        Console.Write("  ");
            //    }
            //    for (int j = 0; j < i + 1; j++)
            //    {
            //        Console.Write("☆");
            //    }
            //    Console.WriteLine();
            //}
            //Console.ReadLine();

 

            //99口诀表
            //for (int i = 1; i <= 9; i++)
            //{
            //    for (int j = 1; j <= i; j++)
            //    {
            //        Console.Write(j+"*"+i+"="+i*j+"\t");
            //    }
            //    Console.WriteLine();
            //}

            //Console.ReadLine();

 

            //循环能够解决的题型
            //穷举    把所有可能的情况都走一遍,使用if筛选出想要的结果
            //百鸡百钱  公鸡2文,母鸡1文,小鸡0.5文
            //int n = 0;
            //for (int i = 0; i * 2 <= 100; i++)
            //{
            //    for (int g = 0; g * 1 <= 100; g++)
            //    {
            //        for (int m = 0; m * 0.5 <= 100; m++)
            //        {
            //            if(i+g+m==100&&i*2+g+m*0.5==100)
            //            {
            //                n++;
            //                Console.WriteLine("公鸡{0}只 ,母鸡{1}只,小鸡{2}只, 一共有几种情况{3}",i,g,m,n);
            //             }
            //        }
            //    }
            //}
            //Console.ReadLine();

            //迭代    根据现有情况不断求解中间情况,最终推导出结果
            //折纸    纸张厚度0.07mm,问对折多少次可以超过珠峰的高度8848m
            //int ci = 0;
            //double zhi = 0.07;
            //while (zhi < 8848000)
            //{
            //    ci++;
            //    zhi *= 2;
            //}
            //Console.WriteLine(ci);
            //Console.ReadLine();


            //while
            //int a = 10;
            //while (a < 16)
            //{
            //    Console.WriteLine("123");
            //    a++;
            //}

            //死循环
            //while (true)
            //{
               
            //}


            //do  while
            //不管你成不成立,先去做了,然后去查看满足与否
            //先斩后奏
            //int a = 10;
            //do
            //{
            //    Console.WriteLine(123);
            //} while (a < 10);

            //Console.ReadLine();


            //break    跳出整个循环
            //continue  跳过本次循环,继续下次循环

//数组:相同数据类型的元素按照一定的顺序进行排列的集合
            //1.数据类型
            //2.数组长度

            //索引是从0开始的
            //一维数组
            //Console.Write("请输入班级人数:");
            //int a = int.Parse(Console.ReadLine());
            //int [] array =new int[a];
            //for (int i = 0; i < a; i++)
            //{
            //    Console.Write("请输入第{0}个人的年龄:",i+1);
            //    array[i] = int.Parse(Console.ReadLine());
            //}
            //Console.WriteLine("所有人员的年龄输入完成,请按回车键继续!");
            //Console.ReadLine();
            //foreach(int aa in array)//遍历集合、数组
            //{
            //    Console.WriteLine(aa);
            //}
            ////没有排序,所以需要冒泡排序
            //for (int i = 0; i < a; i++)
            //{
            //    for (int j = i; j < a - 1; j++)
            //    {
            //        if (array[i] < array[j + 1])//从大到小
            //        {
            //            int zhong = array[i];
            //            array[i] = array[j + 1];
            //            array[j + 1] = zhong;
            //        }
            //    }
            //}
            //foreach (int aa in array)//遍历集合、数组
            //{
            //    Console.WriteLine(aa);
            //}
            //    Console.ReadLine();

            //二维数组
            //int[,] array = new int[,]
            //{
            //    {1,2,3},
            //    {4,5,6},
            //    {7,8,9},
            //    {2,5,8}
            //};

            //for (int i = 0; i < 4; i++)
            //{
            //    for (int j = 0; j < 3; j++)
            //    {
            //        Console.Write(array[i,j]+"\t");
            //    }
            //    Console.WriteLine();
            //}
            //Console.ReadLine();

            //存入每个人的语数英成绩
            //Console.Write("请输入班级人数:");
            //int a = int.Parse(Console.ReadLine());
            //double[,] score = new double[a, 3];
            //for (int i = 0; i < a; i++)
            //{
            //    Console.Write("请输入第{0}个人的语文成绩:",i+1);
            //    score[i, 0] = double.Parse(Console.ReadLine());
            //    Console.Write("请输入第{0}个人的数学成绩:", i + 1);
            //    score[i, 1] = double.Parse(Console.ReadLine());
            //    Console.Write("请输入第{0}个人的英语成绩:", i + 1);
            //    score[i, 2] = double.Parse(Console.ReadLine());
            //}
            //Console.WriteLine("所有成绩输入完毕!请按回车键继续!");
            //Console.ReadLine();

 //集合定义的时候不需要数据类型,不需要长度


            //使用集合之前需要先引用System.Collections这个类
            ArrayList al = new ArrayList();
            al.Add(1);//添加使用add方法,不能直接使用索引号
            al.Add(2);//使用索引号必须在有了这个索引之后才可以进行使用
            al.Add(3);
            al.Add(4);
            al.Add(5);

            //插入1号索引6
            al.Insert(1, "6");

            //Console.WriteLine(al.Count);
            ////移除4这个元素
            //al.Remove(4);//若有多个相同元素,只移除第一个

            ////移除某个索引上的元素
            //al.RemoveAt(3);

            ////第一次出现的索引
            //int index= al.IndexOf(2);
            ////最后一次出现的索引
            //int last = al.LastIndexOf(8);//-1

            ////是否包含
            //bool aaa = al.Contains(2);

            ////默认排序   升序
            //al.Sort();
            ////降序    翻转集合,需要在sort之后进行翻转
            //al.Reverse();

            //克隆
            ArrayList all = new ArrayList();
            all = (ArrayList)al.Clone();
            foreach (object aa in all)
            {
                Console.WriteLine(aa);
            }

 

            Console.ReadLine();