C#系列-流程控制

一、if-else结构

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

namespace _12if_else练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //如果小赵的考试成绩大于90(含)分,那么爸爸奖励他100元钱,
            //否则的话,爸爸就让小赵跪方便面.
            //Console.WriteLine("请输入小赵的考试成绩");
            //int score = Convert.ToInt32(Console.ReadLine());
            //if (score >= 90)
            //{
            //    Console.WriteLine("奖励你一百块");
            //}
            //else
            //{
            //    Console.WriteLine("去跪方便面");
            //}
            //Console.ReadKey();

            //对学员的结业考试成绩评测
            //         成绩>=90 :A
            //90>成绩>=80 :B 	
            //80>成绩>=70 :C
            //70>成绩>=60 :D
            //         成绩<60   :E


            Console.WriteLine("请输入学员的考试成绩");
            int score = Convert.ToInt32(Console.ReadLine());
            //最正确的做法

            if (score >= 90)
            {
                Console.WriteLine("A");
            }
            else if (score >= 80)
            {
                Console.WriteLine("B");
            }
            else if (score >= 70)
            {
                Console.WriteLine("C");
            }
            else if (score >= 60)
            {
                Console.WriteLine("D");
            }
            //else if (score < 60)
            //{
            //    Console.WriteLine("E");
            //}
            else
            {
                Console.WriteLine("E");
            }

            Console.ReadKey();




            #region if的做法
            //if (score >= 90 && score < 100)
            //{
            //    Console.WriteLine("A");
            //}
            //if (score >= 80 && score < 90)//ctrl+k+d
            //{
            //    Console.WriteLine("B");
            //}
            //if (score >= 70 && score < 80)
            //{
            //    Console.WriteLine("C");
            //}
            //if (score >= 60 && score < 70)//98  88
            //{
            //    Console.WriteLine("D");
            //}
            else
            {
                Console.WriteLine("E");
            }
            //if (score < 60)
            //{
            //    Console.WriteLine("E");
            //}
            #endregion
            #region if else-if
            //if (score >= 90)
            //{
            //    Console.WriteLine("A");
            //}
            //else//<90 
            //{
            //    if (score >= 80)
            //    {
            //        Console.WriteLine("B");
            //    }
            //    else//<80
            //    {
            //        if (score >= 70)
            //        {
            //            Console.WriteLine("C");
            //        }
            //        else//<70
            //        {
            //            if (score >= 60)
            //            {
            //                Console.WriteLine("D");
            //            }
            //            else//<60
            //            {
            //                Console.WriteLine("E");
            //            }
            //        }
            //    }
            //} 
            #endregion
            Console.ReadKey();

        }
    }
}

二、if-else练习

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

namespace _练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //比较3个数字的大小 不考虑相等

            //分别的提示用户输入三个数字 我们接受并且转换成int类型
            //Console.WriteLine("请输入第一个数字");
            //int numberOne = Convert.ToInt32(Console.ReadLine());
            //Console.WriteLine("请输入第二个数字");
            //int numberTwo = Convert.ToInt32(Console.ReadLine());
            //Console.WriteLine("请输入第三个数字");
            //int numberThree = Convert.ToInt32(Console.ReadLine());


            //三种情况 应该使用 if else-if来做
            //如果第一个数字大于第二个数字 并且第一个数字还大于第三个数字 
            //if (numberOne > numberTwo && numberOne > numberThree)
            //{
            //    Console.WriteLine(numberOne);
            //}
            如果第二个数字大于第一个数字并且第二个数字大于第三个数字
            //else if (numberTwo > numberOne && numberTwo > numberThree)
            //{
            //    Console.WriteLine(numberTwo);
            //}
            如果第三个数字大于第一个数字并且第三个数字大于第二个数字
            //else
            //{
            //    Console.WriteLine(numberThree);
            //}


            //我先让第一个数字跟第二个数字比较 如果大于第二个数字 再让第一个数字跟第三个数字比较
            //if (numberOne > numberTwo)
            //{
            //    //如果第一个数字大于了第二个数字 再让第一个数字跟第三个数字比较
            //    if (numberOne > numberThree)
            //    {
            //        Console.WriteLine(numberOne);
            //    }
            //    else//第三个数字要大于第一个数字
            //    {
            //        Console.WriteLine(numberThree);
            //    }
            //}
            //else//第二个数字大于了第一个数字
            //{ 
            //    //让第二个数字跟第三个数字进行比较 如果第二个数字大于第三个数字  第二个数字最大 否则第三个数字最大
            //    if (numberTwo > numberThree)
            //    {
            //        Console.WriteLine(numberTwo);
            //    }
            //    else//第三个数字最大
            //    {
            //        Console.WriteLine(numberThree);
            //    }
            //}



            //练习1:提示用户输入密码,如果密码是“88888”则提示正确,否则要求再输入一次,
            //如果密码是“88888”则提示正确,否则提示错误,程序结束。
            //(如果我的密码里有英文还要转换吗,密码:abc1)

            //Console.WriteLine("请输入密码");
            //string pwd = Console.ReadLine();
            //if (pwd == "888888")
            //{
            //    Console.WriteLine("登陆成功");
            //}
            //else//要求用户再输入一次
            //{
            //    Console.WriteLine("密码错误,请重新输入");
            //    pwd = Console.ReadLine();
            //    if (pwd == "888888")
            //    {
            //        Console.WriteLine("输了两遍,终于正确了");
            //    }
            //    else//输入第二次错误
            //    {
            //        Console.WriteLine("两边都不对,程序结束");
            //    }
            //}

            //Console.ReadKey();




            //练习2:提示用户输入用户名,然后再提示输入密码,如果用户名是“admin”并且密码是“88888”,
            //则提示正确,否则,如果用户名不是admin还提示用户用户名不存在,
            //如果用户名是admin则提示密码错误.
            //Console.WriteLine("请输入用户名");
            //string name = Console.ReadLine();
            //Console.WriteLine("请输入密码");
            //string pwd = Console.ReadLine();


            第一种情况:用户名和密码全部输入正确
            //if (name == "admin" && pwd == "888888")
            //{
            //    Console.WriteLine("登陆成功");
            //}
            第二种情况:密码错误
            //else if (name == "admin")
            //{
            //    Console.WriteLine("密码输入错误,程序退出");
            //}
            第三种情况:用户名错误
            //else if (pwd == "888888")
            //{
            //    Console.WriteLine("用户名错误,程序退出");
            //}
            第四种情况:用户名和密码全部错误
            //else
            //{
            //    Console.WriteLine("用户名和密码全部错误,程序退出");
            //}



            //练习3:提示用户输入年龄,如果大于等于18,则告知用户可以查看,如果小于10岁,
            //则告知不允许查看,如果大于等于10岁并且小于18,
            //则提示用户是否继续查看(yes、no),如果输入的是yes则提示用户请查看,
            //否则提示"退出,你放弃查看"。

            //第一种情况  >=18   
            //第二种情况  <10
            //第三种情况  >=10  && <18

            Console.WriteLine("请输入你的年龄");
            int age = Convert.ToInt32(Console.ReadLine());

            if (age >= 18)
            {
                Console.WriteLine("看吧,早晚你都要知道的");
            }
            else if (age < 10)
            {
                Console.WriteLine("滚蛋,回家吃奶去");
            }
            else
            {
                Console.WriteLine("确定要看么?yes/no");
                //input 要么是yes要么是no
                string input = Console.ReadLine();
                if (input == "yes")
                {
                    Console.WriteLine("看吧,早熟的孩子,后果自负哟");
                }
                else//no
                {
                    Console.WriteLine("乖孩子,回家吃奶去吧");
                }
            }

            Console.ReadKey();

        }
    }
}

三、switch-case用法

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

namespace _03switch_case
{
    class Program
    {
        static void Main(string[] args)
        {
            //李四的年终工作评定,如果定为A级,则工资涨500元,如果定为B级,
            //则工资涨200元,如果定为C级,工资不变,如果定为D级工资降200元,
            //如果定为E级工资降500元.
            //设李四的原工资为5000,请用户输入李四的评级,然后显示李四来年的工资
            bool b = true;
            double salary = 5000;
            Console.WriteLine("请输入对李四的年终评定");//a b c d e  乱七八糟
            string level = Console.ReadLine();

            //switch(变量或者表达式的值)
            //{
            //    case 值1:要执行的代码;
            //    break;
            //    case 值2:要执行的代码;
            //    break;
            //    case 值3:要执行的代码;
            //    break;
            //    ..........
            //    default:要执行的代码;
            //    break;
            //}
            switch (level)
            {
                case "A": salary += 500;
                    break;
                case "B": salary += 200;
                    break;
                case "C": break;
                case "D": salary -= 200;
                    break;
                case "E": salary -= 500;
                    break;
                default: Console.WriteLine("输入有误,程序退出");
                    b = false;
                    break;
            }
            if (b)
            {
                Console.WriteLine("李四明年的工资是{0}元", salary);
            }
            Console.ReadKey();


            //ctrl+k+s
            #region if else-if的做法
            //if (level == "A")
            //{
            //    salary += 500;//salary=salary+500;
            //}
            //else if (level == "B")
            //{
            //    salary += 200;
            //}
            //else if (level == "C")
            //{

            //}
            //else if (level == "D")
            //{
            //    salary -= 200;
            //}
            //else if (level == "E")
            //{
            //    salary -= 500;
            //}
            //else//输入的不是ABCDE其中之一
            //{
            //    Console.WriteLine("输入有误,程序退出");
            //    b = false;
            //}
            //if (b)
            //{
            //    Console.WriteLine("李四来年的工资是{0}", salary);
            //} 
            #endregion
            Console.ReadKey();


            //0 0.5 1 1.5 2


        }
    }
}

四、switch-case练习

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

namespace _04switch_case练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //让用户输入姓名,然后显示出这个人上辈子是什么职业。
            //Console.WriteLine("请输入一个姓名,我们将显示出来这个人上辈子的职业");
            //string name = Console.ReadLine();
            老杨、老苏、老马、老蒋、老牛、老虎、老赵
            //switch (name)
            //{
            //    case "老杨": Console.WriteLine("上辈子是抽大烟的");
            //        break;
            //    case "老苏": Console.WriteLine("上辈子是个老鸨子");
            //        break;
            //    case "老马": Console.WriteLine("上辈子是老苏手下的头牌");
            //        break;
            //    case "老蒋": Console.WriteLine("上辈子是拉皮条的");
            //        break;
            //    case "老牛": Console.WriteLine("上辈子是一坨翔");
            //        break;
            //    case "老虎": Console.WriteLine("上辈子是个大病猫");
            //        break;
            //    case "老赵": Console.WriteLine("上辈子是光芒万丈救苦救难的菩萨呀");
            //        break;
            //    default: Console.WriteLine("不认识,估计是一坨翔");
            //        break;
            //}
            //Console.ReadKey();




                     // 成绩>=90 :A      
                     //90>成绩>=80 :B 	
                     //80>成绩>=70 :C
                     //70>成绩>=60 :D
                     //         成绩<60  :E

            Console.WriteLine("请输入一个考试成绩");
            int score = Convert.ToInt32(Console.ReadLine()); //0-100

            switch (score/10)//你要把范围 变成一个定值
            {
                case 10: //case 10要执行的代码跟case 9是一样的
                case 9: Console.WriteLine("A");
                    break;
                case 8: Console.WriteLine("B");
                    break;
                case 7: Console.WriteLine("C");
                    break;
                case 6: Console.WriteLine("D");
                    break;
                default: Console.WriteLine("E");
                    break;
            }
            Console.ReadKey();



        }
    }
}

五、判断闰年

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

namespace _05判断闰年
{
    class Program
    {
        static void Main(string[] args)
        {
            //请用户输年份,再输入月份,输出该月的天数.(结合之前如何判断闰年来做)
            Console.WriteLine("请输入一个年份");
            try
            {
                int year = Convert.ToInt32(Console.ReadLine());
                Console.WriteLine("请输入一个月份");
                try
                {
                    int month = Convert.ToInt32(Console.ReadLine());//1-12
                    if (month >= 1 && month <= 12)
                    {
                        int day = 0;//声明一个变量用来存储天数
                        switch (month)
                        {
                            case 1:
                            case 3:
                            case 5:
                            case 7:
                            case 8:
                            case 10:
                            case 12: day = 31;
                                break;
                            case 2:
                                //由于2月 有平年和闰年的不同  所以首先要判断一下当年是不是闰年
                                if ((year % 400 == 0) || (year % 4 == 0 && year % 100 != 0))
                                {
                                    day = 29;
                                }
                                else
                                {
                                    day = 28;
                                }
                                break;
                            //4 6  9 11
                            default: day = 30;
                                break;
                        }

                        Console.WriteLine("{0}年{1}月有{2}天", year, month, day);
                    }//if判断的括号
                    else
                    {
                        Console.WriteLine("输入的月份不符合要求,程序退出");
                    }
                }//try月份括号
                catch//跟月份配对
                {
                    Console.WriteLine("输入的月份有误,程序退出");
                }
            }//try年份的括号
            catch//跟年份的try配对
            {
                Console.WriteLine("输入的年份有误,程序退出");
            }
            Console.ReadKey();
        }
    }
}

六、while循环结构

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

namespace _06循环结构
{
    class Program
    {
        static void Main(string[] args)
        {
            //向控制台打印100遍 下次考试我一定要细心
            //循环体:Console.WriteLine("下次考试我一定要细心");
            //循环条件:打印的次数小于100遍

            //需要定义一个循环变量用来记录循环的次数 没循环一次,循环变量都应该自身加1
            //int i = 0;
            //while (i < 100)
            //{
            //    Console.WriteLine("下次考试我一定要细心\t{0}",i);
            //    i++;//没循环一次,都要自身加1,否则就是死循环
            //}
            //Console.ReadKey();


            // Console.WriteLine("下次考试我一定要细心");

            //求1-100之间所有整数的和
            //求1-100之间所有偶数的和
            //循环体:累加的过程
            //循环条件:i<=100
            int i = 1;
            int sum = 0;//声明一个变量用来存储总和
            while (i <= 100)
            {
                if (i % 2 != 0)
                {
                    sum += i;

                }
                i++;
            }
            Console.WriteLine(sum);
            Console.ReadKey();





        }
    }
}

七、while循环结构练习

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

namespace _08while循环的3个练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //输入班级人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩
            //10 
            //循环体:提示输入学员成绩,接收,并转换成整数类型,累加到总成绩当中
            //循环条件:循环的次数小于等于班级人数

            //Console.WriteLine("请输入班级人数");
            //int count = Convert.ToInt32(Console.ReadLine());
            //int sum = 0;//用来存放总成绩
            //int i=1;//声明一个循环变量用来记录循环的次数
            //while (i <= count)
            //{
            //    Console.WriteLine("请输入第{0}个学员的考试成绩",i);
            //    int score = Convert.ToInt32(Console.ReadLine());
            //    //表示把每一个学员的成绩累加到总成绩当中
            //    sum += score;
            //    i++;
            //}
            //Console.WriteLine("{0}个人的班级总成绩是{1}平均成绩是{2}",count,sum,sum/count);
            //Console.ReadKey();


            //老师问学生,这道题你会做了吗?如果学生答"会了(y)",
            //则可以放学.如果学生不会做(n),则老师再讲一遍,再问学生是否会做了......
            //直到学生会为止,才可以放学.
            //直到学生会或老师给他讲了10遍还不会,都要放学


            //放学的两个条件:
            //1、会了
            //2、讲完第十遍 不管你会不会 我都放学

            //循环体:老师不停的提问,学生不停的回答,老师还要不停得奖
            //循环条件:学生不会、讲的次数小于10遍
            //string answer = "";
            //int i = 0;
            //while (answer !="yes" && i < 10)
            //{
            //    Console.WriteLine("这是我第{0}遍给你讲,你会了么?yes/no",i+1);
            //    answer = Console.ReadLine();//yes no
            //    //如果学生回答的是 会了 此时应该跳出循环
            //    if (answer == "yes")
            //    {
            //        Console.WriteLine("会了那就放学!!!");
            //        break;
            //    }
            //    i++;
            //}


            //2006年培养学员80000人,每年增长25%,
            //请问按此增长速度,到哪一年培训学员人数将达到20万人?

            //循环体:人数每年增长25%
            //循环条件:人数>=20万

            //double people = 80000;
            //int year = 2006;
            //while (people < 200000)
            //{
            //    people *= 1.25;
            //    year++;
            //}

            //Console.WriteLine("到第{0}年的时候人数将达到20万人",year);

            //Console.ReadKey();



            //提示用户输入yes或者no
            //要求:只能输入yes或者no,只要不是yes或者no就要求用户一直重新输入

            //循环体:提示用户输入 我们接收并且判断
            //循环条件:输入的不能是yes或者no

            //string input = "";//yes
            //while (input != "yes"&& input != "no")
            //{
            //    Console.WriteLine("请输入yes或者no");
            //    input = Console.ReadLine();
            //}



            //提示用户输入用户名和密码 要求用户名等于admin密码等于888888
            //只要用户名错误或者密码错误就重新输入
            //但是,最多只能输入3次

            //循环体:提示用户输入用户名和密码  接收  判断
            //循环条件:用户名或者密码错误  最多错误3次

            //int i = 1;
            //string userName = "";
            //string userPwd = "";
            //while ((userName != "admin" || userPwd != "888888") && i <= 3)
            //{
            //    Console.WriteLine("请输入用户名");
            //    userName = Console.ReadLine();
            //    Console.WriteLine("请输入密码");
            //    userPwd = Console.ReadLine();
            //    i++;
            //}
            //Console.ReadKey();


            //写两个循环
            //第一个循环提示用户A输入用户名 要求A的用户名不能为空,只要为空,就要求A一直重新输入

            //循环体:提示A输入用户名 接收  判断
            //循环条件:用户名为空

            string userNameA = "";
            while (userNameA == "")
            {
                Console.WriteLine("请输入用户名,不能为空");
                userNameA = Console.ReadLine();
            }
            //  Console.ReadKey();



            //第二个循环提示用户B输入用户名,要求B的用户名不能跟A的用户名相同 并且不能为空
            //只要为空,并且跟A的用户名相同,就一直提示用户B重新输入用户名


            //循环体:提示输入B的用户名 接收判断
            //循环条件:用户名为空 或者跟A的相同

            Console.WriteLine("请输入用户名,不能跟A相同,并且不能为空");
            string userNameB = Console.ReadLine();
            while (userNameB == "" || userNameB == userNameA)
            {
                if (userNameB == "")
                {
                    Console.WriteLine("用户名不能为空,请重新输入");
                    userNameB = Console.ReadLine();
                }
                else
                {
                    Console.WriteLine("用户名B不能跟A的用户名相同,请重新输入");
                    userNameB = Console.ReadLine();
                }
            }

            Console.ReadKey();





        }
    }
}

八、do-while循环结构

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

namespace _09do_while循环
{
    class Program
    {
        static void Main(string[] args)
        {
            //明天小兰就要登台演出了,老师说再把明天的演出的歌曲唱一遍,
            //如果满意,小兰就可以回家了.否则就需要再练习一遍,直到老师满意为止.(y/n)

            //循环体:小兰唱了一遍 问老师 满意么?老师回答
            //循环条件:老师不满意
            //Console.WriteLine("老师我唱的你满意么?");
            //string answer = Console.ReadLine();
            //while (answer == "no")
            //{
            //    Console.WriteLine("老师,我再唱一遍,你满意么?");
            //    answer = Console.ReadLine();
            //}
            //遇见这种首先执行一遍循环体,拿着执行后的结果再去判断是否执行循环的循环。
            //我们推荐使用do-while循环。
            //string answer = "";
            //do
            //{
            //    Console.WriteLine("老师,我唱的你满意么?yes/no");
            //    answer=Console.ReadLine();
            //}while(answer=="no");
            //Console.WriteLine("OK,放学回家~~~");
            //Console.ReadKey();


            //要求用户输入用户名和密码,只要不是admin、888888就一直提示用户名或密码错误,请重新输入
            //string name = "";
            //string pwd = "";
            //do
            //{
            //    Console.WriteLine("请输入用户名");
            //    name = Console.ReadLine();
            //    Console.WriteLine("请输入密码");
            //    pwd = Console.ReadLine();
            //} while (name != "admin" || pwd != "888888");
            //Console.WriteLine("登陆成功");
            //Console.ReadKey();


            //练习3:不断要求用户输入学生姓名,输入q结束.

            string name = "";
            while (name != "q")
            {
                Console.WriteLine("请输入学员姓名,输入q结束");
                name = Console.ReadLine();
            }
            Console.ReadKey();

            //do
            //{
            //    Console.WriteLine("请输入学员姓名,输入q结束");
            //    name = Console.ReadLine();
            
            //}while(name!="q");
            //Console.ReadKey();


        }
    }
}

九、break用法

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

namespace _07_break关键字的用法
{
    class Program
    {
        static void Main(string[] args)
        {
            //int i = 1;
            //int j=1;
            //while (i <= 10)
            //{
            //    while (j <= 10)
            //    {
            //        Console.WriteLine("我是里面的while循环");
            //        j++;
            //        break;
            //    }
            //    Console.WriteLine("我是外面的while循环");
            //    i++;
            //}
            //Console.ReadKey();


            //int i = 0;
            //while (i < 10)
            //{
            //    Console.WriteLine("Hello 酷狗");
            //    break;
            //}
            //Console.ReadKey();

            // 要求用户输入用户名和密码 用户名只要不是admin 密码不是888888
            //循环体:提示用户输入用户名 我们接收  密码 接收 判断是否登陆成功
            //循环条件:用户名或密码错误
            string userName = "";
            string userPwd = "";
            while (userName != "admin" || userPwd != "888888")
            {
                Console.WriteLine("请输入用户名");
                userName = Console.ReadLine();
                Console.WriteLine("请输入密码");
                userPwd = Console.ReadLine();
            }
            Console.WriteLine("登陆成功");
            Console.ReadKey();
        }
    }
}

十、异常捕获

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

namespace _02异常捕获
{
    class Program
    {
        static void Main(string[] args)
        {
            //语法上没有错误,在程序运行的过程当中,由于某些原因程序出现了错误,不能再正常的运行。
            bool b = true;
            int number=0;//声明了一个变量
            Console.WriteLine("请输入一个数字");
            try
            {
                //abc
                 number = Convert.ToInt32(Console.ReadLine());//赋值
               
            }
               // Console.WriteLine("fdsfdsfds");
            catch
            {
                Console.WriteLine("输入的内容不能够转换成数字");
                b = false;
            }
            //我们如果要执行下面这行代码,需要满足某些条件。
            //让代码满足某些条件去执行的话,使用bool类型
            if (b)
            {
                Console.WriteLine(number * 2);//使用
            }
            Console.ReadKey();
        }
    }
}

十一、以上总结

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

namespace _01复习
{
    class Program
    {
        static void Main(string[] args)
        {
            //练习4:不断要求用户输入一个数字,然后打印这个数字的二倍,当用户输入q的时候程序退出。
            //循环体:提示用户输入一个数字 接收 转换  打印2倍
            //循环条件:输入的不能是q

            //string input = "";
            //while (input != "q")
            //{
            //    Console.WriteLine("请输入一个数字,我们将打印这个数字的2倍");
            //    //不能直接转换成int类型 因为用户有可能输入q
            //    input = Console.ReadLine();//数字 q 乱七八糟
            //    if (input != "q")
            //    {
            //        try
            //        {
            //            int number = Convert.ToInt32(input);
            //            Console.WriteLine("您输入的数字的2倍是{0}", number * 2);
            //        }
            //        catch
            //        {
            //            Console.WriteLine("输入的字符串不能够转换成数字,请重新输入");
            //        }
            //    }
            //    else//==q
            //    {
            //        Console.WriteLine("输入的是q,程序退出");
            //    }
            //}

            //练习5:不断要求用户输入一个数字(假定用户输入的都是正整数),当用户输入end的时候显示刚才输入的数字中的最大值
            //循环体:提示用户输入一个数字  接收  转换成int类型  不停的比较大小
            //循环条件:输入的不能是end
            //F11
            string input = "";
            int max = 0;
            while (input != "end")
            {
                Console.WriteLine("请输入一个数字,输入end我们将显示你输入的最大值");
                input = Console.ReadLine();//数字 end  乱七八糟
                if (input != "end")
                {
                    try
                    {
                        int number = Convert.ToInt32(input);
                        //让用户输入的每个数字都跟我假定的最大值比较,只要比我假定的最大值要大,
                        //就把当前输入的这个数字赋值给我的最大值
                        if (number > max)
                        {
                            max = number;
                        }
                    }
                    catch
                    {
                        Console.WriteLine("输入的字符串不能够转换成数字,请重新输入");
                    }
                   
                }
                else//==end
                {
                    Console.WriteLine("您刚才输的数字中的最大值是{0}",max);
                }
            }
            Console.ReadKey();


            Console.ReadKey();
        }
    }
}

十二、for循环

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

namespace _03for循环
{
    class Program
    {
        static void Main(string[] args)
        {
            //向控制台打印10遍

            for (int i = 0; i < 10; i++)
            {
                Console.WriteLine("欢迎{0}", i);
            }
            //for (int i = 0; i < length; i++)
            //{
                
            //}
            Console.ReadKey();

            //int i = 0;//定义循环的次数
            //while (i < 10)
            //{
            //    Console.WriteLine("欢迎");
            //    i++;
            //}
            //Console.ReadKey();
        }
    }
}

十三、for循环的正序输出和倒序输出

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

namespace _04for循环的正序输出和倒序输出
{
    class Program
    {
        static void Main(string[] args)
        {
            //请打印 1-10
            int i = 0;
            for (; i < 10; )
            {
                Console.WriteLine("欢迎");
                i++;
            }
            Console.ReadKey();

            //for (int i = 1; i <= 10; i++)
            //{
            //    Console.WriteLine(i);
            //}
            打印10-1
            //for (int i = 10; i >= 1; i--)
            //{
            //    Console.WriteLine(i);
            //}

            //for (int i = 10; i >= 1; i--)
            //{
            //    Console.WriteLine(i);
            //}
            Console.ReadKey();
        }
    }
}

十四、for练习一

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

namespace _05for循环的练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //求1-100之间的所有整数和   偶数和  奇数和
            //int sum = 0;
            //int n = 100;
            //for (int i = 1; i <= n; i += 2)
            //{
            //    sum += i;
            //}
            //Console.WriteLine(sum);
            //Console.ReadKey();

            //找出100-999间的水仙花数?
            //水仙花数指的就是 这个百位数字的
            //百位的立方+十位的立方+个位的立方==当前这个百位数字
            //153  1 125  27  153  i
            //百位:153/100
            //十位:153%100/10
            //个位:153%10

            for (int i = 100; i <= 999; i++)
            {
                int bai = i / 100;
                int shi = i % 100 / 10;
                int ge = i % 10;
                if (bai * bai * bai + shi * shi * shi + ge * ge * ge == i)
                {
                    Console.WriteLine("水仙花数有{0}",i);
                }
            }
            Console.ReadKey();

        }
    }
}

十五、for嵌套

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

namespace _06for循环的嵌套
{
    class Program
    {
        static void Main(string[] args)
        {
            

            //当遇到某个事情要做一遍,而另外一个事情要做N遍的时候
            //for循环的嵌套
            for (int i = 0; i < 10; i++)
            {
                for (int j = 0; j < 10; j++)
                {
                    Console.WriteLine("Hello World i循环了{0}次,j循环了{1}次",i,j);
                    break;
                }
            }
            Console.ReadKey();
        }
    }
}

十六、for循环练习二

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

namespace _09for循环的3个练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //练习1:循环录入5个人的年龄并计算平均年龄,
            //如果录入的数据出现负数或大于100的数,立即停止输入并报错.
            //int sum = 0;
            //bool b = true;
            //for (int i = 0; i < 5; i++)
            //{
            //    Console.WriteLine("请输入第{0}个人的成绩",i+1);
            //    try
            //    {
            //        int age = Convert.ToInt32(Console.ReadLine());
            //        if (age >= 0 && age <= 100)
            //        {
            //            sum += age;
            //        }
            //        else
            //        {
            //            Console.WriteLine("输入的年龄不在正确范围内,程序退出!!!");
            //            b = false;
            //            break;
            //        }
            //    }
            //    catch
            //    {
            //        Console.WriteLine("输入的年龄不正确,程序退出!!!");
            //        b = false;
            //        break;
            //    }
            //}
            //if (b)
            //{
            //    Console.WriteLine("5个人的平均年龄是{0}", sum / 5);
            //}
            //Console.ReadKey();


       //     练习2:在while中用break实现要求用户一直输入用户名和密码,
            //只要不是admin、88888就一直提示要求重新输入,如果正确则提登录成功.
            //string name = "";
            //string pwd = "";
            //while (true)
            //{
            //    Console.WriteLine("请输入用户名");
            //    name = Console.ReadLine();
            //    Console.WriteLine("请输入密码");
            //    pwd = Console.ReadLine();

            //    if (name == "admin" && pwd == "888888")
            //    {
            //        Console.WriteLine("登陆成功");
            //        break;
            //    }
            //    else
            //    {
            //        Console.WriteLine("用户名或密码错误,请重新输入");
            //    }
            //}
            //Console.ReadKey();


            //1~100之间的整数相加,得到累加值大于20的当前数
            //(比如:1+2+3+4+5+6=21)结果6 sum>=20  i
            int sum = 0;
            for (int i = 1; i <= 100; i++)
            {
                sum += i;
                if (sum >= 20)
                {
                    Console.WriteLine("加到{0}的时候,总和大于了20",i);
                    break;
                }
            }
            Console.ReadKey();


        }
    }
}

十七、乘法口诀表

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

namespace _07乘法口诀表
{
    class Program
    {
        static void Main(string[] args)
        {
            //for (int i = 1; i <= 9; i++)
            //{
            //    for (int j = 1; j <= i; j++)
            //    {
            //        Console.Write("{0}*{1}={2}\t", i, j, i * j);
            //    }
            //    Console.WriteLine();//换行
            //}
            //Console.ReadKey();

            //Console.Write("Hello Wor\tld");
            //Console.WriteLine();
            //Console.Write("Hello World");
            //Console.ReadKey();

            Console.WriteLine("请输入一个数字");
            int number = Convert.ToInt32(Console.ReadLine());


            for (int i = 0; i <=number; i++)
            {
                Console.WriteLine("{0}+{1}={2}",i,number-i,number);
            }
            Console.ReadKey();
        }
    }
}

十八、continue使用

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

namespace _11continue练习
{
    class Program
    {
        static void Main(string[] args)
        {
            //练习1:用 while continue实现计算1到100(含)之间的除了能被7整除之外所有整数的和。

            //int sum = 0;
            //int i=1;
            //while (i <= 100)
            //{
            //    if (i % 7 == 0)
            //    {
            //        i++;
            //        continue;
            //    }
            //    sum += i;
            //    i++;
            //}
            //Console.WriteLine(sum);
            //Console.ReadKey();


            //找出100内所有的素数
            //素数/质数:只能被1和这个数字本身整除的数字
            //2 3  4  5  6  7
            //7   7%1   7%2 7%3 7%4 7%5 7%6  7%7  6%2
           
            for (int i = 2; i <= 100; i++)
            {
                bool b = true;
                for (int j = 2; j <i; j++)
                {
                    //除尽了说明不是质数 也就没有再往下继续取余的必要了
                    if (i % j == 0)
                    {
                        b = false;
                        break;
                    }
                }

                if (b)
                {
                    Console.WriteLine(i);
                }
            }

            Console.ReadKey();
            //6   6%2 6%3
        }
    }
}

十九、三元表达式

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

namespace _12三元表达式
{
    class Program
    {
        static void Main(string[] args)
        {
            计算两个数字的大小 求出最大的
            //Console.WriteLine("请输入第一个数字");
            //int n1 = Convert.ToInt32(Console.ReadLine());
            //Console.WriteLine("请输入第二个数字");
            //int n2 = Convert.ToInt32(Console.ReadLine());
                        语法:
            表达式1?表达式2 :表达式3
            //int max = n1 > n2 ? n1 : n2;
            //Console.WriteLine(max);
            if (n1 > n2)
            {
                Console.WriteLine(n1);
            }
            else
            {
                Console.WriteLine(n2);
            }
            //Console.ReadKey();


            //提示用户输入一个姓名 只要输入的不是老赵  就全是流氓
            Console.WriteLine("请输入姓名");
            string name = Console.ReadLine();

            string result = name == "老赵" ? "淫才呀" : "流氓呀";
            Console.WriteLine(result);
            Console.ReadKey();

            //if (name == "老赵")
            //{
            //    Console.WriteLine("淫才呀");
            //}
            //else
            //{
            //    Console.WriteLine("流氓呀");
            //}
            Console.ReadKey();


        }
    }
}

二十、随机数

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

namespace _13_随机数
{
    class Program
    {
        static void Main(string[] args)
        {
            //产生随机数
            //1、创建能够产生随机数的对象
            //Random r = new Random();
            //while (true)
            //{

            //    //2、让产生随机数的这个对象调用方法来产生随机数
            //    int rNumber = r.Next(1, 11);
            //    Console.WriteLine(rNumber);
            //    Console.ReadKey();
            //}

            //输入名字随机显示这个人上辈是什么样的人
            //思路:
            //1、创建能够产生随机数的对象
            //2、产生随机数 (1,7)

            Random r = new Random();
            while (true)
            {
                int rNumber = r.Next(1, 7);
                Console.WriteLine("请输入一个姓名");
                string name = Console.ReadLine();
                switch (rNumber)
                {
                    case 1: Console.WriteLine("{0}上辈子是吃翔的", name);
                        break;
                    case 2: Console.WriteLine("{0}上辈子是拉翔的", name);
                        break;
                    case 3: Console.WriteLine("{0}上辈子就是一坨翔", name);
                        break;
                    case 4: Console.WriteLine("{0}上辈子是大汉奸", name);
                        break;
                    case 5: Console.WriteLine("{0}上辈子是拉皮条的", name);
                        break;
                    case 6: Console.WriteLine("{0}上辈子是救苦救难的活菩萨", name);
                        break;
                }
                Console.ReadKey();
            }
           
        }
    }
}

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

南叔先生

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值