3月18日 全部练习题(一)

//1.输入三个整数,xyz,最终以从小到大的方式输出。利用if嵌套。

  Console.Write("请输入x=");
           int x = int.Parse(Console.ReadLine());
           Console.Write("请输入y=");
           int y = int.Parse(Console.ReadLine());
           Console.Write("请输入z=");
           int z = int.Parse(Console.ReadLine());        
            if (x < y && x < z)
            {
                Console.WriteLine(x);
                if (y < z)
                {
                    Console.WriteLine(y);
                    Console.WriteLine(z);
                }
                else
                {
                    Console.WriteLine(z);
                    Console.WriteLine(y);
                }
            }
            else if (y < x && y < z)
            {
                Console.WriteLine(y);
                if (x < z)
                {
                    Console.WriteLine(x);
                    Console.WriteLine(z);
                }
                else
                { 
                    Console.WriteLine(z);
                    Console.WriteLine(x);
                }
            }
            else
            {
                Console.WriteLine(z);
                if (x < y)
                { 
                    Console.WriteLine(x);
                    Console.WriteLine(y);
                }
                else
                { 
                    Console.WriteLine(y);
                    Console.WriteLine(x);
                }
            }
            Console.ReadLine();

 //2.输入三个整数,xyz,最终以从小到大的方式输出。利用中间变量。

 Console.Write("请输入x=");
            int x = int.Parse(Console.ReadLine());
            Console.Write("请输入y=");
            int y = int.Parse(Console.ReadLine());
            Console.Write("请输入z=");
            int z = int.Parse(Console.ReadLine());
            int zhong;                      
            if (x > y)
            {
                zhong = x;
                x = y;
                y = zhong;
            }
            if (x > z)
            {
                zhong = x;
                x = z;
                z = zhong;
            }
            if (y > z)
            {
                zhong = y;
                y = z;
                z = zhong;
            }
            Console.WriteLine(x);
            Console.WriteLine(y);
            Console.WriteLine(z);
            Console.ReadLine();

//3.输入三个整数,xyz,最终以从小到大的方式输出。利用条件运算符。

Console.Write("请输入x=");
            int x = int.Parse(Console.ReadLine());
            Console.Write("请输入y=");
            int y = int.Parse(Console.ReadLine());
            Console.Write("请输入z=");
            int z = int.Parse(Console.ReadLine());
            int a = x < y && x < z ? 1 : 2;
            if (a == 1)
            {
                if (y < z)
                {
                    Console.WriteLine(x + "<" + y + "< " + z);
                }
                else
                {
                    Console.WriteLine(x + " <" + z + "< " + y);
                }
            }
            else
            {
                int b = y < x && y < z ? 1 : 2;
                if (b == 1)
                {
                    if (x < z)
                    {
                        Console.WriteLine(y + " <" + x + " <" + z);
                    }
                    else
                    {
                        Console.WriteLine(y + "<" + z + " <" + x);
                    }
                }
                else//z最小
                {
                    if (x < y)
                    {
                        Console.WriteLine(z + "<" + x + " <" + y);
                    }
                    else
                    {
                        Console.WriteLine(z + " <" + y + " <" + x);
                    }
                }
            } 
            Console.ReadLine();

//4.“现在几点了?”键盘键入小时数,判断是上午还是下午。
       打印出来现在是上午几点还是下午几点。利用条件运算符。

 Console.WriteLine("现在是几点了?");
            int a = int.Parse(Console.ReadLine());
            if (a >= 0 && a <=24)
            {
                string b = a < 12 ? "现在是" + a+"am." : "现在是" + (a - 12)+"pm.";
                Console.WriteLine(b);
            }
            else
            {
                Console.WriteLine("您的输入有误!");
            }
            Console.ReadLine();

//5.相亲过程:你有房子么?你有钱么?你有能力么?
    【结婚吧】【先买房子在结婚】【先赚钱再买房子再结婚】都没有【拜拜~~】
   利用if嵌套做相亲过程。

            Console.Write("女问:你有房子吗?");
            string a = Console.ReadLine();
            if (a == "")
            {
                Console.WriteLine("那我们结婚吧!");
            }
            else
            {
                Console.Write("女问:你有钱吗?");
                string b = Console.ReadLine();
                if (b == "")
                {
                    Console.WriteLine("先买房子在结婚!");
                }
                else
                {
                    Console.Write("女问:你有能力么?");
                    string c = Console.ReadLine();
                    if (c == "")
                    {
                        Console.WriteLine("先赚钱再买房子再结婚!");
                    }
                    else
                    {
                        Console.WriteLine("拜拜");
                    }
                }
            }
            Console.ReadLine();
            Console.Write("女问:你有房子吗?");
            string a = Console.ReadLine();
            if (a == "")
            {
                Console.WriteLine("那我们结婚吧!");
            }
            else
            {
                Console.Write("女问:你有钱吗?");
                string b = Console.ReadLine();
                if (b == "")
                {
                    Console.WriteLine("先买房子在结婚!");
                }
                else
                {
                    Console.Write("女问:你有能力么?");
                    string c = Console.ReadLine();
                    if (c == "")
                    {
                        Console.WriteLine("先赚钱再买房子再结婚!");
                    }
                    else
                    {
                        Console.WriteLine("拜拜");
                    }
                }
            }
            Console.ReadLine();
            Console.Write("女问:你有房子吗?");
            string a = Console.ReadLine();
            if (a == "")
            {
                Console.WriteLine("那我们结婚吧!");
            }
            else
            {
                Console.Write("女问:你有钱吗?");
                string b = Console.ReadLine();
                if (b == "")
                {
                    Console.WriteLine("先买房子在结婚!");
                }
                else
                {
                    Console.Write("女问:你有能力么?");
                    string c = Console.ReadLine();
                    if (c == "")
                    {
                        Console.WriteLine("先赚钱再买房子再结婚!");
                    }
                    else
                    {
                        Console.WriteLine("拜拜");
                    }
                }
            }
            Console.ReadLine();

 // 6.输入年月日,看看格式是否正确。利用if嵌套。  

 for (; ; )
            {
                Console.Write("请输入年份:");
                int year = int.Parse(Console.ReadLine());
                if (year >= 0 && year <= 9999)
                {
                    for (; ; )
                    {
                        Console.Write("请输入月份:");
                        int month = int.Parse(Console.ReadLine());
                        if (month > 0 && month <= 12)
                        {
                            for (; ; )
                            {
                                Console.Write("请输入日期:");
                                int day = int.Parse(Console.ReadLine());
                                if (day > 0 && day <= 31)
                                {
                                    if (month == 1 || month == 3 || month == 5 || month == 7 || month == 8 || month == 10 || month == 12)
                                    {
                                        Console.WriteLine("您输入的格式正确!现在是" + year + "" + month + "" + day + "日。");
                                    }
                                    else if (month == 4 || month == 6 || month == 9 || month == 11)
                                    {
                                        if (day <= 30)
                                        {
                                            Console.WriteLine("您输入的格式正确!现在是" + year + "" + month + "" + day + "日。");
                                        }
                                        else
                                        {
                                            Console.WriteLine("您输入的日期有误!请重新输入!");
                                        }
                                    }
                                    else
                                    {
                                            if (day <= 28)
                                            {
                                                Console.WriteLine("您输入的格式正确!现在是" + year + "" + month + "" + day + "日。");
                                            }
                                            else
                                            {
                                                if (day == 29)
                                                {
                                                    if (year % 4 == 0 && year % 100 == 0 || year % 400 == 0)
                                                    {
                                                        Console.WriteLine("您输入的格式正确!现在是" + year + "" + month + "" + day + "日。");
                                                    }
                                                }
                                                else
                                                {
                                                    Console.WriteLine("您输入的日期有误!请重新输入!");
                                                }
                                            }                                        
                                    }
                                    break;
                                }
                                else
                                {
                                    Console.WriteLine("您输入的日期有误!请重新输入!");
                                }
                            }
                            break;
                        }
                        else
                        {
                            Console.WriteLine("您输入的月份有误!请重新输入!");
                        }
                    }
                    break;
                }
                else
                {
                    Console.WriteLine("您输入的年份有误!请重新输入!");
                }
            }           
             Console.ReadLine();

 

//7.输入年月日,看看格式是否正确。利用DateTime。

 

  Console.WriteLine("请输入日期:");
            try
            {
                DateTime a = DateTime.Parse(Console.ReadLine());
                Console.WriteLine("您输入的格式正确!");
            }
            catch
            {
                Console.WriteLine("您输入的格式不正确!");
            }
           
            Console.ReadLine();

 

 // 8.做人机猜拳,剪刀石头布。利用switch case。

  for (; ; )
            {
                Console.WriteLine("现在开始猜拳游戏");
                Console.Write("1.剪刀");
                Console.Write("2.石头");
                Console.Write("3.布");
                Console.WriteLine();
                Console.Write("请出拳:");
                int i = int.Parse(Console.ReadLine());
                Random ran = new Random();
                int a = ran.Next(1, 4);
                if (a > 0 && a < 4)
                {
                    switch (a)
                    {
                        case 1:
                            Console.WriteLine("机器人出拳:1.剪刀");
                            break;
                        case 2:
                            Console.WriteLine("机器人出拳:2.石头");
                            break;
                        case 3:
                            Console.WriteLine("机器人出拳:3.布");
                            break;
                    }
                }
                Console.ReadLine();
                if (i > 0 && i < 4)
                {
                    if (i - a == -1 || i - a == 2)
                    {
                        Console.WriteLine("很抱歉您输了,请继续");
                    }
                    else if (i - a == 1 || i - a == -2)
                    {
                        Console.WriteLine("恭喜您获胜,请继续");
                    }
                    else
                    {
                        Console.WriteLine("我们打平了,请继续");
                    }
                }
                else
                {
                    Console.WriteLine("您的输入有误");
                    break;
                }
            }
            Console.ReadLine();

 // 9.输入一个正整数,求1!+2!+3!+...+n!。利用for循环嵌套。

 Console.Write("请输入一个正整数:");
            int n = int.Parse(Console.ReadLine());
            int sum = 0;
            for (int i = 1; i <= n; i++)
            {
                int sum1 = 1;
                for (int j = 1; j <= i; j++)
                {
                    sum1 *= j;
                }
                sum += sum1;
            }
            Console.WriteLine(sum);
            Console.ReadLine();

 

// 10.找出100以内与7有关的数并打印,并求出他们的和。利用for循环+if。

         (a%7==0||a%10==7||a/10==7)

 int sum = 0;
            for (int a = 1; a <= 100; a++)
            {
                if (a % 7 == 0 || a % 10 == 7 || a / 10 == 7)
                {
                    Console.WriteLine(a);
                    sum += a;
                }
            }
            Console.WriteLine("他们的和是:"+sum);
            Console.ReadLine();

// 11.一个游戏,前20关是每一关自身的分数,
         21-30关每一关是10分,
         31-40关,每一关是20分,
         41-49关,每一关是30分,
         第50关是100分,
         输入你现在闯到的关卡数,求你现在拥有的分数。利用for嵌套if。

 Console.Write("请输入您现在闯到的关数:");
            int n = int.Parse(Console.ReadLine());
            int sum = 0;
            if (n > 0 && n <= 50)
            {
                for (int i = 1; i <= n; i++)
                {
                    if (i >= 1 && i <= 20)
                    {
                        sum += i;
                    }
                    else if (i >= 21 && i <= 30)
                    {
                        sum += 10;
                    }
                    else if (i >= 31 && i <= 40)
                    {
                        sum += 20;
                    }
                    else if (i >= 41 && i <= 49)
                    {
                        sum += 30;
                    }
                    else
                    {
                        sum += 100;
                    }
                }
                Console.WriteLine(sum);
            }
            else
            {
                Console.WriteLine("您的输入有误!");
            }         
            Console.ReadLine();

// 12.一个游戏,前20关是每一关自身的分数,
         21-30关每一关是10分,
         31-40关,每一关是20分,
         41-49关,每一关是30分,
         第50关是100分,
         输入你现在闯到的关卡数,求你现在拥有的分数。利用if嵌套for。

            Console.Write("请输入您现在所闯到的关卡数:");
            int a = int.Parse(Console.ReadLine());
            int sum = 0;
            if (a >= 1 && a <= 50)
            {
                if (a >= 1 && a <= 20)
                {
                    for (int i = 1; i <= a; i++)
                    {
                        sum += i;
                    }
                }
                else if (a >= 21 && a <= 30)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= a; i++)
                    {
                        sum += 10;
                    }
                }
                else if (a >= 31 && a <= 40)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= 30; i++)
                    {
                        sum += 10;
                    }
                    for (int i = 31; i <= a; i++)
                    {
                        sum += 20;
                    }
                }
                else if (a >= 41 && a <= 49)
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= 30; i++)
                    {
                        sum += 10;
                    }
                    for (int i = 31; i <= 40; i++)
                    {
                        sum += 20;
                    }
                    for (int i = 41; i <= a; i++)
                    {
                        sum += 30;
                    }
                }
                else
                {
                    for (int i = 1; i <= 20; i++)
                    {
                        sum += i;
                    }
                    for (int i = 21; i <= 30; i++)
                    {
                        sum += 10;
                    }
                    for (int i = 31; i <= 40; i++)
                    {
                        sum += 20;
                    }
                    for (int i = 41; i <= 49; i++)
                    {
                        sum += 30;
                    }
                    sum += 100;
                }
            }
            else
            {
                Console.WriteLine("输入有误!");
            }
            Console.WriteLine("总分为:" + sum);

 

posted @ 2016-03-18 16:28  陌上初薰  阅读(213)  评论(0编辑  收藏  举报