黑马程序员--基础视频中的主要练习题及代码

---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------

    第一次看苏坤老师讲的课程就慢慢的喜欢上了c# ,以下就是我总结课程里面的一些主要练习题代码:

1:

        static void Main(string[] args)
        {

            #region 1)编程实现计算几天(如46天)是几周零几天?  2)编程实现N秒(如107653秒)是几天几小时几分几秒?   让用户输入!!
            uint weeks, days1, days2, hours, minutes, seconds;  //定义周数、天数1、天数2、小时、分钟、秒 注意因为这些数据都不能包含负数,所以定义为uint类型
            Console.WriteLine("请输入你需要计算的天数?");
            try
            {
                uint Days = Convert.ToUInt32(Console.ReadLine());//接收用户输入的天数,并且转换为uint类型
                weeks = Days / 7;
                days1 = Days % 7;
                Console.WriteLine("N天有{0}周零{1}天", weeks, days1);
            }
            catch
            {
                Console.WriteLine("请输入的天数是正整数且该整数不大于4,294,967,295!");
            }

            Console.WriteLine("请输入你要计算的秒数?");
            try
            {
                uint Seconds = Convert.ToUInt32(Console.ReadLine());//接收并转换输入的秒数
                days2 = Seconds / 86400;         //得到天数
                hours = (Seconds % 86400) / 3600; //得到小时
                minutes = ((Seconds % 86400) % 3600) / 60;//得到分钟
                seconds = ((Seconds % 86400) % 3600) % 60; //得到秒数
                Console.WriteLine("N秒有{0}天{1}小时{2}分钟{3}秒", days2, hours, minutes, seconds);
            }
            catch
            {
                Console.WriteLine("输入的数据应该是正整数且不大于4,294,967,295");
            }


            //不需要考虑 2/3=?几的问题,因为定义是时候是uint类型,所以2/3=0;
            #endregion

            //让用户输入一个年份,如果是闰年,则输出true,否则输出false
            //判断条件:1)年份能够被400整除 years%400==0 或者满足条件:2)年份能够被4整除但不能被100整除 years%4==0&&years%!=0
            #region 3:
            Console.WriteLine("请输入要判定的年份?");
            try
            {
                uint years = Convert.ToUInt32(Console.ReadLine());
                if ((years % 400 == 0) || ((years % 4 == 0) && (years % 100 != 0)))
                {
                    Console.WriteLine(true);
                }
                else
                {
                    Console.WriteLine(false);
                }
            }
            catch
            {
                Console.WriteLine("输入的年份应该为正整数且不大于4,294,967,295");
            }
            #endregion

            /*提示用户输入年龄,如果大于等于18,则告知用户可以查看,如果小于10岁,则告知用户不允许查看,
             *如果大于等于10岁,则提示用户是否继续查看(yes、no),如果输入的是yes则提示用户请查看,否则提示"退出,你放弃查看".
             */
            #region 4:
            Console.WriteLine("请输入你的年龄");
            try
            {
                uint age = Convert.ToUInt32(Console.ReadLine());
                if (age >= 18 && age <= 110)
                {
                    Console.WriteLine("可以查看!");
                }
                else if (age < 10)
                {
                    Console.WriteLine("你年龄过小,不能查看!");
                }
                else
                {
                    Console.WriteLine("是否要查看(输入yes或no)");
                    string input = Console.ReadLine();
                    if (input == "yes")
                    {
                        Console.WriteLine("请查看");
                    }
                    else
                    {
                        Console.WriteLine("退出,你放弃查看");
                    }
                }
            }
            catch
            {
                Console.WriteLine("输入的年龄不是正整数!");
            }
            #endregion


            #region 5:让用户输入年份和月份,输出该月的天数!
            Console.WriteLine("请输入要计算的年份");
            try
            {
                uint inputYear = Convert.ToUInt32(Console.ReadLine());
                Console.WriteLine("请要查询的输入月份");
                uint inputMonth = Convert.ToUInt32(Console.ReadLine());

                bool secondMonth;   //定义是否为闰年的变量
                if (inputYear % 400 == 0 || (inputYear % 4 == 0 && inputYear % 100 != 0)) //判断是否为闰年
                {
                    secondMonth = true;  //闰年为true
                    OutputDays(inputMonth, secondMonth);
                }
                else
                {
                    secondMonth = false; //不是闰年则为false
                    OutputDays(inputMonth, secondMonth);
                }
            }
            catch
            {
                Console.WriteLine("输入的年份或者月份不正确!");
            }
            #endregion

            //对学员的结业成绩进行评级(用switch来做)成绩>=90:A  90>成绩>=80:B 80>成绩>=70:C 70>成绩>=60:D 成绩<60:E
            //原本使用if-else更好,但是他们之间刚好都是差10
            #region 6:
            Console.WriteLine("输入你的成绩!(0-100间)");
            try
            {
                int score = Convert.ToInt32(Console.ReadLine());
                switch (score / 10)
                {
                    case 10:
                    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;
                }
            }
            catch
            {
                Console.WriteLine("请输入0-100的整数!");
            }
            #endregion
            Console.ReadKey();
        }
        /// <5:输出相应的月份的天数方法  第五道题的相应方法>
        ///
        ///
        static void OutputDays(uint month,bool sMonth)
        {
            switch (month)
            {
                case 1:
                case 3:
                case 5:
                case 7:
                case 8:
                case 10:
                case 12:
                    Console.WriteLine("你要查询的月份天数是:31天");
                    break;
                case 4:
                case 6:
                case 9:
                case 11:
                    Console.WriteLine("你要查询的月份天数的:30天");
                    break;
                default:
                    if (sMonth == true)
                    {
                        Console.WriteLine("你要查询的月份是:28天");  //闰年的2月份是28天
                    }
                    else
                    {
                        Console.WriteLine("你要查询的月份是:29天"); //不是闰年的2月份是29天
                    }
                    break;
            }
        } 
    }

2:

    class Program
    {
        static void Main(string[] args)
        {
            //老师问学生,这道题你会做了吗?如果学生回答"会了(y)",则放学,否则回答不会"(n)",则老师再讲一遍,再问...
            //1)直到学生回答会为止才放学 2)或者老师讲了10遍还不会都要放学
            #region 1: do-while循环体
            int i = 0;   //定义老师讲的次数,初始值为0
            bool ask = true;//循环条件
            do    //至少老师要问一次
            {
                Console.WriteLine("会做了?(输入y[会]/n[不会])");
                string answer = Console.ReadLine(); //接收输入的是y还是n
                i++;
                if (answer == "y" || i == 10)  //回答是y或者讲完10次都要放学
                {
                    Console.WriteLine("放学!");
                    ask = false;     //此时循环条件要赋予false,让其推出循环
                }
                else if (answer != "n")  //写这一条件是为了提示用户不能输入除y或n以外的任何字符,否则退出程序!
                {
                    Console.WriteLine("输入的字符只能是y或者n");
                    Console.WriteLine("按下任何键退出,重新运行!");
                    break;
                }
            } while (ask);
            #endregion


            #region 2://2006年培养学员8万人,每年增长25%,请问按此增长速度,到哪一年培养学员人数达到20万人?
            int years = 0, students = 80000;  //定义要计算的开始年份,学生人数为8万
            bool s = true;                    //没有达到20万人数时,循环条件为true,达到了则为false
            while (s)
            {
                if (students < 200000)
                {
                    years++;
                    students = Convert.ToInt32(80000 * Math.Pow(1.25, years)); //使用数学Math类来计算              
                                                                                                          /* double stu = 80000;
                                                                                                             while(true)
                                                                                                             {
                                                                                                                 stu *= 1.25; 这种算法也可以像1.25的N次幂
                                                                                                             }  */
                }
                else
                {
                    years = years + 2006;
                    Console.WriteLine(years + "年达到20万");
                    s = false;  //达到20人数退出循环
                }
            }
            #endregion


            #region  3.输入班级人数,然后依次输入学员成绩,计算班级学员的平均成绩和总成绩!
            Console.WriteLine("输入你们班级的人数?");
            try
            {
                uint students1 = Convert.ToUInt32(Console.ReadLine());  //接收输入的总人数,正整数
                Console.WriteLine("你们班的人数为{0}人,请依次输入每个同学的成绩!", students1);
                int i1 = 0;
                double[] score1 = new double[students1];  //定义一个数组作为每个学员的成绩
                double sum = 0;                //定义学员的总成绩,初始值为0
                while (i1 < students1)
                {
                    Console.WriteLine("请输入第{0}个同学的成绩", i1 + 1);
                    score1[i1] = Convert.ToDouble(Console.ReadLine());  //依次接收学员的成绩
                    sum += score1[i1];      //依次累加每个学员的总成绩
                    i1++;
                    if (i1 == students1)   //为等到输入完学员成绩后再输出,设定了这个条件
                    {
                        Console.WriteLine("班级的学员平均成绩为{0},总成绩为{1}", sum, sum / students1);
                        break;
                    }
                }
            }
            catch
            {
                Console.WriteLine("请输入的人数为正整数");
            }
            #endregion
            Console.ReadKey();
        }
    }

3:

    class Program
    {
        static void Main(string[] args)
        {
            //1:用while计算1-100之间的整数和
            int i=1, sum=0;
            while (i <= 100)
            {
                sum += i;
                i++;
            }
            Console.WriteLine("1-100之间的整数和是{0}",sum);

            //#region 2://要求用户输入用户名和密码,只要不是admin和888888就一直提示用户名和密码错误,请重新输入。
            //bool input = true;  //循环条件
            //while (input)
            //{
            //    Console.WriteLine("请输入用户名");
            //    string userName = Console.ReadLine();
            //    Console.WriteLine("请输入密码");
            //    string password = Console.ReadLine();
            //    if (userName == "admin")
            //    {
            //        if (password == "888888")
            //        {
            //            input = false;
            //        }
            //        else
            //        {
            //            Console.WriteLine("你输入的密码有误,请重新输入!");
            //        }
            //    }
            //    else
            //    {
            //        Console.WriteLine("你输入的用户名错误,请重新输入!");
            //    }
            //}
            //#endregion

            //不断要求用户输入学生姓名,输入q结束
            //bool bool3=true;
            //while(bool3)
            //{
            //    Console.WriteLine("同学,请输入你的名字!");
            //    string input3=Console.ReadLine();
            //    if(input3=="q")
            //    {
            //        Console.WriteLine("按任意键结束!");
            //        bool3 = false;
            //    }
            //}

            //不断要求用户输入一个数字,然后打印这个数字的二倍,当用户输入q的时候程序退出
            //bool bool4 = true;
            //while (bool4)
            //{
            //    Console.WriteLine("请输入一个数字");
            //    string input4 =Console.ReadLine();
            //    if (input4 == "q")
            //    {
            //        Console.WriteLine("输入任意键退出程序!");
            //        bool4 = false;
            //    }
            //    else
            //    {
            //        int Input4 = Convert.ToInt32(input4); //转换为int类型
            //        Console.WriteLine("这个数字的两倍是{0}", Input4 * 2);
            //    }
            //}


            //#region 4:不断要求用户输入一个数字,当用户输入end的时候显示刚才输入的数字的最大值
            //uint Max = 0;   //赋予开始的最大值为0
            //uint num;       //输出要比较的最大值
            //string input5 = "";  //接收输入的数字,开始为空
            //try
            //{
            //    while (true)
            //    {
            //        Console.WriteLine("请输入一个数字");
            //        input5 = Console.ReadLine();
            //        num = Convert.ToUInt32(input5);  //转换输入的字符串为Uint类型,假许输入字符串就直接跳到try-catch语句,
            //        //则直接在catch语句输出最大值,如果刚开始就输入"end"字符,则最大值为0,输入的不是"end"字符串不能得到最大值!
            //        if (num > Max)
            //        {
            //            Max = num;
            //        }
            //    }
            //}
            //catch
            //{
            //    Console.WriteLine("输入的不是正整数");
            //    if (input5 == "end")       //我们可以在catch语句输出最大值
            //    {
            //        Console.WriteLine("最大值是{0},按任意键退出!", Max);
            //    }
            //    else
            //    {
            //        Console.WriteLine("输入的不是\"end\"字符串不能显示最大值");
            //    }
            //}
            //#endregion


            #region 5:张三先唱一遍要表演的歌曲,老师觉得张三唱歌不过关,就让张三再唱一遍,老师满意则可以下课,否则再唱,直到老师满意
            bool bool6 = true;
            do
            {
                Console.WriteLine("老师我跳的怎么样啊?,老师回答过关/不过关(y/n)");
                string answer = Console.ReadLine();
                if (answer == "y")
                {
                    Console.WriteLine("可以了,下课!");
                    bool6 = false;
                }
            } while (bool6);
            #endregion
            Console.ReadKey();

4:

    class Program
    {
        static void Main(string[] args)
        {
            //1:求1-100间的所有偶数和?
            #region 1:--
            int sum = 0;
            for (int i1 = 1; i1 <= 100; i1++)
            {
                if (i1 % 2 == 0)
                {
                    sum += i1;
                }
            }
            Console.WriteLine("1-100间的所有偶数和是{0}", sum);
            #endregion

            //2:找出100-999间的水仙花数(水仙花数是指一个 n 位数 ( n≥3 ),它的每个位上的数字的 n 次幂之和等于它本身)?
            //(例如:1^3 + 5^3 + 3^3 = 153)
            #region 2--
            int a, b, c; //分别代表百位、十位、个位
            for (a = 1; a <= 9; a++)  //注意a能是0
            {
                for (b = 0; b <= 9; b++)
                {
                    for (c = 0; c <= 9; c++)
                    {
                        if (100 * a + 10 * b + c == a * a * a + b * b * b + c * c * c) 
                        {
                            Console.WriteLine("水仙花数分别是{0}", 100 * a + 10 * b + c);
                        }
                    }
                }
            }
            #endregion
           
            //3:输出九九乘法
            int i, j;  //分别代表行数和列数
            for (i = 1; i <= 9; i++)
            {
                for (j = 1; j<=i; j++)
                {
                    Console.Write("{0}*{1}={2}", i, j, i * j);  //也可以补位{2:00}
                    Console.Write("\t");  //写这一句话的作用是每输出一句话留个空格
                }
                Console.Write("\n");  //写这一句的作用是没循环一遍的外循环时换行
            }

            //4:1-100之间的整数相加,得到累加值大于20的当前数
            int sum2 = 0;
            for (int i2 = 1; i2 <= 100; i2++)
            {
                 sum2+=i2;
                 if (sum2 > 20)
                 {
                     Console.WriteLine("当前的整数是{0}", i2);
                     break; //得到当期数立即跳出所以的循环  continue(立即结束本次循环)是有区别的
                 }
            }

            //5:用while continue实现计算1-100之间的除了能被7整除之外所有整数的和
            int sum3 = 0;
            int i3=0;
            while(i3<100)
            {
                i3++;           //一定要注意递加不能在continue后面,不然出现死循环
                if (i3 % 7 == 0)
                {
                    continue;   //立即结束本次循环(而老师的答案有问题)结果应该是:4315
                }
                sum3 += i3;
            }
            Console.WriteLine("1-100之间的除了能被7整除之外所有整数的和是{0}", sum3);
            Console.ReadKey();
        }
    }

5:

        static void Main(string[] args)
        {
            ////1:从一个整数数组中取出最大的整数
            //Console.WriteLine("输入要查询的数组个数");
            //int num=Convert.ToInt32(Console.ReadLine());
            //int[] inputs1=new int[num];  //定义一个数组接收输入的每个数字
            //int i1 = 0;
            //while (i1 < num)
            //{
            //    try
            //    {
            //        Console.WriteLine("输入第{0}个要比较的数字", i1 + 1);
            //        inputs1[i1] = Convert.ToInt32(Console.ReadLine());
            //    }
            //    catch
            //    {
            //        Console.WriteLine("请输入一个整数");
            //    }
            //    i1++;
            //}
            //int Max = inputs1[0];
            //for (i1= 0; i1 < num; i1++)
            //{
            //    if (inputs1[i1] > Max)   //比较每个数值的大小
            //    {
            //        Max = inputs1[i1];
            //    }
            //}
            //Console.WriteLine("输入的整数中,最大的是"+Max);
            ////2:计算一个整数数组的所有元素的和
            //int sum1 = 0;
            //for (i1 = 0; i1 < num; i1++)
            //{
            //    sum1 += inputs1[i1];
            //}
            //Console.WriteLine("输入数组的元素和是" + sum1);
            ////3:将一个字符串数组输出为|分割的形式,比如"梅西|卡卡|郑大世"
            //string[] names = { "张三", "李四", "赵武", "黄六", "钱七" };
            //int i3;
            //string sumName = "";
            //for (i3 = 0; i3 < names.Length-1; i3++)
            //{
            //    sumName += names[i3] + "|";
            //}
            //sumName += names[names.Length - 1];  //最后一个名字没有竖杠
            ////string.Join("|", names);  也可以使用Join()方法
            //Console.WriteLine(sumName);
            //4:将一个整数数组的每一个元素进行如下处理:如果元素是正数则将这个位置的元素的值加1,
            //如果元素是负数则将这个位置的元素的值减1,如果是0,则不变
            int[] numbers = { 10, 30, -11, 25, 90,0, -40, 80, -100, 94 ,0};
            for (int i5 = 0; i5 < numbers.Length; i5++)
            {
                if (numbers[i5] > 0)
                {
                    numbers[i5] = numbers[i5] + 1;
                }
                else if (numbers[i5] == 0)
                {
                    numbers[i5] = numbers[i5];
                }
                else
                {
                    numbers[i5] = numbers[i5] - 1;
                }
            }
            foreach (int numb in numbers)
            {
                Console.WriteLine(numb);
            }
            //5:将一个字符串数组的元素的顺序进行反转。如{"3","a","8","haha"}--{"haha","8","a","3"}
            //第i个和第length-i-1个进行交换
            string[] strs = { "aa", "ad", "你好", "要过年了", "什么时候回家?" };
            string temp = "";  //定义一个接收交换变量
            for (int i4 = 0; i4 < strs.Length / 2; i4++)
            {
                temp = strs[i4];
                strs[i4] = strs[strs.Length - i4 - 1];
                strs[strs.Length - i4 - 1] = temp;
            }
            //Array.Reverse(strs);  也可以使用数组类的Reverse方法 --反转
            foreach (string str in strs)
            {
                Console.Write(str+",");
            }
            Console.ReadKey();
        }

6:

    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("输入的是"+Input());
           
            Console.ReadKey();
        }
        //练习,写方法:
        //1:读取输入的整数,定义成方法,多次调用(如果用户输入的是数字,则返回,否则提示用户重新输入)
        static int Input()
        {
            while (true)
            {
                Console.WriteLine("请输入一个整数!");
                try
                {
                    int output = Convert.ToInt32(Console.ReadLine());
                    return output;
                }
                catch
                {
                    Console.WriteLine("输入的不是一个整数,请重新输入!");
                }
            }
        }
        //2:查找两个整数中的最大值:int Max(int i1,int i2)
        int Max(int i1, int i2)
        {
            if (i1 > i2)
            {
                return i1;
            }
            return i2;  //两个数值相等或I2比较大则返回i2
        }
        //3:计算输入数组的和:int Sum(int[] values)
        int Sum(int[] values)
        {
            int sum = 0;
            for (int i = 0; i < values.Length; i++)
            {
                sum += values[i];
            }
            return sum;
        }
    }

7:

    class Program
    {
        static void Main(string[] args)
        {
            double[] numbers = { 1.2, 50, 63, 42.5, 60, 12.53, 61.44, 23 };
            MaoPao(numbers);
            double oMax=0, oMin;
            double sum = Method(numbers,ref oMax,out oMin);   //ref必须赋值 out不需要赋值,赋值也没有意义
            Console.WriteLine("{0}-{1}-{2}",sum, oMax, oMin);


            Method();
            Console.ReadKey();
           
        }
        //冒泡排序(由大到小):第一遍过后最大/最小值在后面(要比较N-1次) ....第N-1遍后就只比较1次了
        static void MaoPao(double[] values)
        {
            double temp;
            for (int i = 0; i < values.Length;i++ )
            {
                for (int j = 0; j < values.Length-i-1;j++ )   //这个条件已经限定j不能等于0
                {
                    temp=values[j];
                    if (values[j] < values[j+1])   //   由大到小排序<  由小到大则> 只有这里有区别
                    {
                        values[j] = values[j + 1];  //假许前者小于后者,则前者的位置为后者是数据
                        values[j+1] = temp;       //后者的位置则为前者的数据
                    }
                }
            }
            foreach (double outPut in values)
            {
                Console.WriteLine(outPut);
            }
        }

        //写一个方法,计算int类型数组中每个元素的总和和最大值和最小值
        static double Method(double[] values, ref double Max,out double Min)  //注意形参:数组 out ref定义方法都需要写数据类型
        {
            double sum1=0;
            Max = values[0];
            Min = values[0];  //out方法里必须赋值
            for(int i=0;i             {
                sum1+=values[i]; //累加数组元素
            }
            for (int i = 1; i < values.Length; i++)
            {
                if (values[i] > Max)  //判断数组元素大小,大的赋予Max
                {
                    Max = values[i];
                }
            }
            for (int i = 1; i < values.Length; i++) //判断数组元素大小,小的赋予Min
            {
                if (Min > values[i])
                {
                    Min = values[i];
                }
            }
            return sum1;
        }

        //重复让用户输入一个数,判断是否为质数,输入q结束 ()
        static bool ZhiShu(uint number)
        {
            for (int i = 2; i < number; i++)
            {
                if (number % i != 0)    //输入的数字不能被2到本身-1的数整除,则为质数
                {
                    return true;
                }              
            }
            return false;
        }
        static void Method()
        {
            while (true)
            {
                Console.WriteLine("输入一个正整数");
                string inputN = "";
                try
                {
                    inputN=Console.ReadLine();
                    uint num = Convert.ToUInt32(inputN);
                    bool bool1= ZhiShu(num);  //调用方法且传入输入的数字
                    //判断方法返回的是true还是false
                    if (bool1 == true)
                    {
                        Console.WriteLine("你输入的是一个质数");
                    }
                    else
                    {
                        Console.WriteLine("你输入的你不是质数");
                    }
                }
                catch
                {
                    //当输入的不是正整数时,跳入catch语句
                    if (inputN == "q")
                    {
                        Console.WriteLine("结束");
                        break;
                    }
                    else
                    {
                        Console.WriteLine("输入的不是q字符串,请重新输入!");
                    }                  
                }
            }
        }
    }

以上的代码写得有点乱,望理解,谢谢......

---------------------- Windows Phone 7手机开发、.Net培训、期待与您交流! ----------------------

你可能感兴趣的:(黑马程序员--基础视频中的主要练习题及代码)