码迷,mamicode.com
首页 > 其他好文 > 详细

5月3日 条件语句、循环语句的复习练习

时间:2016-05-04 01:11:15      阅读:220      评论:0      收藏:0      [点我收藏+]

标签:

练习一

        //用户输入姓名,然后打印此人的年龄,从1岁-18岁,每一岁占一行,打印内容为“我今年xxx岁了!”;

        //当6岁时增加打印“我上小学了!”;

        //当11岁时增加打印“我上初中了!”;

        //当15岁时增加打印“我上高中了!”;

        //当18岁时增加打印“我成年了!考上了北大!”;

 

 

1用for循环

            Console.Write("请输入您的姓名:");

            string name = Console.ReadLine();

            for (int i = 1; i <= 18; i++)

            {

                Console.WriteLine("我叫" + name + ",我今年" + i + "岁了!");

                switch (i)

                {

                    case 6: Console.WriteLine("我上小学了!"); break;

                    case 11: Console.WriteLine("我上初中了!"); break;

                    case 15: Console.WriteLine("我上高中了!"); break;

                    case 18: Console.WriteLine("我成年了!考上北大了!"); break;

                }

                

            }

            Console.ReadLine();

2用for嵌套if

            Console.Write("请输入您的姓名:");

            string name = Console.ReadLine();

            for (int nl = 1; nl <= 18; nl++)

            {

                if (nl == 6)

                {

                    Console.WriteLine("我叫" + name + ",我今年" + nl + "岁了!我上小学了!");

                }

                else if (nl == 11)

                {

                    Console.WriteLine("我叫" + name + ",我今年" + nl + "岁了!我上初中了!");

                }

                else if (nl == 15)

                {

                    Console.WriteLine("我叫" + name + ",我今年" + nl + "岁了!我上高中了!");

                }

                else if (nl == 18)

                {

                    Console.WriteLine("我叫" + name + ",我今年" + nl + "岁了!我成年了!考上北大了!");

                }

                else

                {

                    Console.WriteLine("我叫" + name + ",我今年" + nl + "岁了!");

                }

            }

            Console.ReadLine();

练习二

//输入三个数从大到小排列打印(x、y、z)

            Console.Write("请输入第一个数:");

            int x = int.Parse(Console.ReadLine());

            Console.Write("请输入第二个数:");

            int y = int.Parse(Console.ReadLine());

            Console.Write("请输入第三个数:");

            int z = int.Parse(Console.ReadLine());

            if (x > y && x > z)

            {

                if (y > z)

                {

                    Console.WriteLine("三位数的排序为:");

                    Console.WriteLine(x);

                    Console.WriteLine(y);

                    Console.WriteLine(z);

                }

                else 

                {

                    Console.WriteLine("三位数的排序为:");

                    Console.WriteLine(x);

                    Console.WriteLine(z);

                    Console.WriteLine(y);

                }                

            }

            else if (y > z && y > x)

            {

                if (z > x)

                {

                    Console.WriteLine("三位数的排序为:");

                    Console.WriteLine(y);

                    Console.WriteLine(z);

                    Console.WriteLine(x);

                }

                else 

                {

                    Console.WriteLine("三位数的排序为:");

                    Console.WriteLine(y);

                    Console.WriteLine(x);

                    Console.WriteLine(z);

                }                

            }

            else if (z > x && z > y)

            {

                if (x > y)

                {

                    Console.WriteLine("三位数的排序为:");

                    Console.WriteLine(z);

                    Console.WriteLine(x);

                    Console.WriteLine(y);

                }

                else 

                {

                    Console.WriteLine("三位数的排序为:");

                    Console.WriteLine(z);

                    Console.WriteLine(y);

                    Console.WriteLine(x);

                }

            }

            else if (x == y)

            {

                if (x < z)

                {

                    Console.WriteLine("三位数的排序为:");

                    Console.WriteLine(z);

                    Console.WriteLine(x);

                    Console.WriteLine(y);

                }

                else

                {

                    Console.WriteLine("三位数的排序为:");

                    Console.WriteLine(x);

                    Console.WriteLine(y);

                    Console.WriteLine(z);

                }

            }

            else if (x == z)

            {

                if (x < y)

                {

                    Console.WriteLine("三位数的排序为:");

                    Console.WriteLine(y);

                    Console.WriteLine(x);

                    Console.WriteLine(z);

                }

                else

                {

                    Console.WriteLine("三位数的排序为:");

                    Console.WriteLine(x);

                    Console.WriteLine(z);

                    Console.WriteLine(y);

                }

            }

            else if (z == y)

            {

                if (z > x)

                {

                    Console.WriteLine("三位数的排序为:");

                    Console.WriteLine(z);

                    Console.WriteLine(y);

                    Console.WriteLine(x);

                }

                else

                {

                    Console.WriteLine("三位数的排序为:");

                    Console.WriteLine(x);

                    Console.WriteLine(y);

                    Console.WriteLine(z);

                }

            }

            Console.ReadLine();

练习三:

//打印100以内的所有质数,并求质数的个数以及总和

            int count = 0;

            int sum = 0;

            int gs = 0;//质数的个数

            for (int i = 1; i <= 100; i++)

            {

                count=0;//进入for循环的次数,count=2则为质数

                for (int j = 1; j <= i; j++)

                {

                    if (i % j == 0)//质数是只能被1和自身整除,判断是否能够整除,整除的话加1

                    {

                        count++;

                    }

                }

                if (count == 2)//判断内循环走完后,外循环走完之前count是否等于2

                {

                    Console.Write(i + ",");

                    gs++;//质数的个数

                    sum += i;

                }

            }

            Console.WriteLine();

            Console.WriteLine("100以内所有质数的个数是:"+gs++);

            Console.WriteLine("100以内所有质数的总和为:"+sum);

            Console.ReadLine();

练习四:

 //让用户循环操作,用户输入一个正整数(0-20),如果小于0或大于20都提示输入错误,

            //如果输入的是0-20之间的数,那么就打印从0到这个数的累加求和,

            //一共需要用户输入3遍,输入错误不计入这3遍操作当中

int count =0;

            //用户循环输入

            for (int i = 0; i <= 20; i++)

            {

            //判断count是否等于3,题目要求输入三遍

                if (count>=3)

                {

                break;

                }

                    Console.WriteLine("请输入一个正整数");

                int a=int.Parse(Console.ReadLine());

                //判断用户输入的数

                if (a>=0&&a<=20)

                {

                    //输入正确,累加求和

                    int sum=0;

                    for (int j = 0; j <= a; j++)

                    {

                        sum +=j;

                    }

                        Console.WriteLine(sum);

                        count++;

                        Console.WriteLine("您输入的是第"+count+"遍");                

                }

                else 

                {

                Console.WriteLine("您的输入有误!");

                }            

            }

            Console.ReadLine();

练习五:

 //打印方形,每行打印10个“A”,打印10行,使用循环嵌套,不允许定义内容为“AAAAAAAAAA”的变量;

 

            for (int i = 1; i <= 10; i++)//打印多少行

            {

                string sum = "A";//定义为字符串类型,疑问:可以定义其他类型吗?

                for (int a = 1; a <= 10; a++)//每行打印多少个

                {

                    sum += "A";

                }

                Console.WriteLine(sum);

            }

            Console.ReadLine();

练习六:

//100以内奇数的和

int sum = 0;

            for (int i = 1; i <= 100; i++)

            {

                if (i % 2 != 0)

                {

                    sum += i;

                }

            }

            Console.WriteLine(sum);

            Console.ReadLine();

练习七:

//请输入一个整数(0--100),如果小于30,则打印(30以内)

            //如果大于等于30小于等于80,则打印(30--80)

            //如果大于80,则打印大于80,如果小于0或大于100,则返回(输入错误)

Console.Write("请输入一个整数(0-100):");

            int a = int.Parse(Console.ReadLine());

            if (a >= 1 && a <= 29)

            {

                Console.WriteLine("您输入的数是:" + a + ",小于30");

            }

            else if (a >= 30 && a <= 80)

            {

                Console.WriteLine("您输入的数是:" + a + ",小于等于80");

            }

            else if (a >= 81 && a <= 100)

            {

                Console.WriteLine("您输入的数是:" + a + ",大于80");

            }

            else

            {

                Console.WriteLine("您的输入有误!");

            }

            Console.ReadLine();

练习八:

//有一张超大的纸,纸张的厚度是0.07毫米,对折多少次可以达到珠峰的高度,按照8848来计算

            //8848米=8848000毫米

练习九:

1)打印三角形

*      *****      *****      *          *           

**      ****      ****      **         ***          

***      ***      ***      ***        *****        

****      **      **      ****       *******        

*****      *      *      *****      *********        

(2)菱形

      *

     ***

   *****

 *******

   *****

    ***

      *

 

 

 

(3)用户输入一个奇数,打印上面最后的菱形,最长的个数是用户输入的个数

练习十:

//猜拳

            //三局两胜,电脑连赢两局,或者玩家连赢两局,2局结束

            //电脑赢一局,玩家赢一局,3局

            //平局,不知道几局才能结束了,

            //只要是电脑或是玩家赢了2局,那么就结束,否则继续

            //break结束循环

            //continue结束当前次循环,进入下一次循环

 

 

//用户输入石头剪子布,计算器也随机出一个石头剪子布,

                //用户与计算机进行对比,得出胜负;

 

                //1、用户输入选择的手势

                Console.Write("请输入您的手势(石头:0,剪子:1,布:2):");

                int yh = Convert.ToInt32(Console.ReadLine());

 

                //2、计算机随机选出计算机的手势

                Random r = new Random();

                int dn = r.Next(0, 3);

 

                //3、对比,输出结果

                //石头0,剪子1,布2

                //0 1 = -1

                //1 2 = -1

                //2 0 = 2

 

                //0 2 = -2

                //1 0 = 1

                //2 1 = 1

                int jg = yh - dn;

                if (jg == -1 || jg == 2)

                {

                    Console.WriteLine("用户胜利!");

                }

                else if (jg == -2 || jg == 1)

                {

                    Console.WriteLine("用户失败!");

                }

                else

                {

                    Console.WriteLine("平局!");

                }

                Console.ReadLine();

 

5月3日 条件语句、循环语句的复习练习

标签:

原文地址:http://www.cnblogs.com/juyangchao12/p/5456998.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!