天天看点

C#基础-045 练习题

/*
          1、求出1—100内的所有质数的和。(质数:只能被1和本身整除(),1不是质数也不是合数)
         */
        public static void Task01()
        {
            int sum = ;
            for (int i = ; i < ; i++)
            {
                if (IsPrime(i))
                {
                    //Console.WriteLine(i);
                    sum += i;
                }
            }
            Console.WriteLine("1—100内的所有质数的和为{0}", sum);
            //Console.ReadKey();
        }
        static bool IsPrime(int r)
        {
            for (int i = ; i <= r / ; i++)
            {
                if (r % i == )
                {
                    return false;
                }
            }
            return true;
        }
           
/*  2、打印图形,例如传入2 
          输出
          AA
          BBBB
          传入4 输出
          AA
          BBBB
          CCCCCC
          DDDDDDDD
         */
        public static void Task02()
        {
            Console.WriteLine("请输入要打印的行数");
            int lines = int.Parse(Console.ReadLine());
            for (int i = ; i <= lines; i++)
            {
                for (int j = ; j <  * i; j++)
                {
                    Console.Write((char)('A' + i - ));
                }
                Console.WriteLine();
            }
        }
           
/*  3、写一个方法把字符串中的大写字母转换成小写,小写字母转化成大写。
         */
        public static void Task03()
        {
            string str = "sffsDdssgCHHJHVJHVJHDDSDFGdg";
            Console.WriteLine("大小写翻转后为{0}", Conversion(str));
        }
        static string Conversion(string str)
        {
            char word;
            string words = string.Empty;
            for (int i = ; i < str.Length; i++)
            {
                word = str[i];
                if (word >= 'A' && word < 'Z')
                {

                    words += char.ToLower(word);
                }
                else if (word >= 'a' && word < 'z')
                {

                    words += char.ToUpper(word); ;
                }
            }
            return words;
        }
           
/* 4、合并数组 把两个数组中索引相同的元素进行相加
           例如:
                arr1 = {1,2,3,4,5,6}
                arr2 = {2,3,4,5,6,7,8}
           合并后 {3,5,7,9,11,13,8}
          【注意两个数组的长度不确定  第一个数组的长度可能大于第二个数组的长度】
       */
        public static void Task04()
        {
            Random ran = new Random();
            Console.WriteLine("第一个随机数组:");
            int[] numberArr1 = new int[ran.Next(, )];
            for (int i = ; i < numberArr1.Length; i++)
            {
                int a = ran.Next(, );
                if (!(IsContains(a, numberArr1)))
                {
                    numberArr1[i] = a;
                }
            }
            Show(numberArr1);

            Console.WriteLine("第二个随机数组:");
            int[] numberArr2 = new int[ran.Next(, )];
            for (int i = ; i < numberArr2.Length; i++)
            {
                int b = ran.Next(, );
                if (!(IsContains(b, numberArr2)))
                {
                    numberArr2[i] = b;
                }
            }
            Show(numberArr2);

            int bigLength = ;
            int smallLength = ;
            if (numberArr1.Length > numberArr2.Length)
            {
                smallLength = numberArr2.Length;
                bigLength = numberArr1.Length;
            }
            else
            {
                bigLength = numberArr2.Length;
                smallLength = numberArr1.Length;
            }

            int[] resultArr = new int[bigLength];
            for (int i = ; i < smallLength; i++)
            {
                resultArr[i] = numberArr1[i] + numberArr2[i];
            }
            for (int i = smallLength; i < bigLength; i++)
            {
                if (numberArr1.Length > numberArr2.Length)
                {
                    resultArr[i] = numberArr1[i];
                }
                else
                {
                    resultArr[i] = numberArr2[i];
                }
            }

            Console.WriteLine("合并后的结果为:");
            Show(resultArr);
        }
        static bool IsContains(int number, int[] arr)
        {
            for (int i = ; i < arr.Length; i++)
            {
                if (arr[i] == number)
                {
                    return true;
                }
            }
            return false;
        }
        static void Show(int[] arr)
        {
            for (int i = ; i < arr.Length; i++)
            {
                Console.Write("{0}  ", arr[i]);
            }
            Console.WriteLine();
        }
           
/*
        5、根据规律写方法
        1 1 2 3 5 8 13 21。。。
        传入6
        返回13

        传入7
        返回21
        */
        public static void Task05()
        {
            Console.WriteLine("请输入一个大于二的整数,回车键得到相应的斐波那契数");
            int num = int.Parse(Console.ReadLine());
            Console.WriteLine(FabniceNumber(num));
        }
        static int FabniceNumber(int num)
        {
            int[] number = new int[num + ];
            number[] = ;
            number[] = ;
            for (int i = ; i < num + ; i++)
            {
                number[i] = number[i - ] + number[i - ];
            }
            return number[num];
        }
           
/*
        6、打印杨辉三角
                     1
                    1 1
                   1 2 1 
                  1 3 3 1
                 1 4 6 4 1
               1 5 10 10 5 1
        */
        public static void Task06()
        {
            Console.WriteLine("请输入需要打印的杨辉三角的行数");
            int num = int.Parse(Console.ReadLine());
            int[,] arr = new int[num, num];
            for (int i = ; i < num; i++)
            {
                for (int j = ; j < num - i; j++)
                {
                    Console.Write("    ");
                }
                for (int j = ; j <= i; j++)
                {
                    //第一种方法
                    //if (j == 0 || j == i)
                    //{
                    //    arr[i, j] = 1;
                    //}
                    //else
                    //{
                    //    arr[i, j] = arr[i - 1, j] + arr[i - 1, j - 1];
                    //}
                    //方法二:三目运算
                    arr[i, j] = j ==  || j == i ?  : arr[i - , j] + arr[i - , j - ];
                    Console.Write(arr[i, j].ToString() + "      ");
                }
                Console.WriteLine();
            }
            Console.ReadKey();
        }
           
/*
        7、统计字符串中的数字字符,大写字母,小写字母的个数  【注 out】
       */
        public static void Task07()
        {
            string str = "afH856";
            int numberCount = ;
            int bigWordsCount = ;
            int smallWordsCount = ;
            Count(str, out bigWordsCount, out smallWordsCount,out numberCount);
            //方法1
            //for (int i = 0; i < str.Length; i++)
            //{
            //    if (str[i] >= 'A' && str[i] < 'Z')
            //    {
            //        bigWordsCount++;
            //    }
            //    else if (str[i] >= 'a' && str[i] < 'z')
            //    {
            //        smallWordsCount++;
            //    }
            //    else if ( str[i] >= '0' && str[i] <= '9')
            //    {
            //        numberCount++;
            //    }
            //}
            Console.WriteLine("{0}中有大写字母{1}个,小写字母{2}个,数字{3}个", str, bigWordsCount, smallWordsCount, numberCount);
        }
        //方法二
        static void Count(string str, out int bigWordsCount, out int smallWordsCount ,out int numberCount)
        {
            numberCount = ;
            bigWordsCount = ;
            smallWordsCount = ;
            for (int i = ; i < str.Length; i++)
            {
                if (str[i] >= 'A' && str[i] < 'Z')
                {
                    bigWordsCount++;
                }
                else if (str[i] >= 'a' && str[i] < 'z')
                {
                    smallWordsCount++;
                }
                else if (str[i] >= '0' && str[i] <= '9')
                {
                    numberCount++;
                }
            }
        }
           
/*
        8、编写一个学生类:
           学生类包括:
           字段:名字、年龄、所属省份、所属市
           测试:
           1产生5个学生对象。字段值通过构造器自定义即可。
           2根据省份输出学员中该省份的所有学生信息。
           3根据所属市输出学员中该市的所有学生信息。
       */

        public static void Task08()
        {
            Student[] stu = new Student[];
            stu[] = new Student("小明", , "河南省", "郑州");
            stu[] = new Student("小红", , "河北省", "石家庄");
            stu[] = new Student("小张", , "河南省", "开封");
            stu[] = new Student("小李", , "山东省", "济南");
            stu[] = new Student("小宋", , "山东省", "聊城");
            Console.WriteLine("-----------------------------");
            ProvinceShow(stu, "山东省");
            ProvinceShow(stu, "河北省");
            ProvinceShow(stu, "河南省");
            Console.WriteLine("-----------------------------");
            CityShow(stu, "开封");
            CityShow(stu, "济南 ");
            CityShow(stu, "石家庄");

        }
        static void ProvinceShow(Student[] arr, string province)
        {
            for (int i = ; i < arr.Length; i++)
            {
                if (province == arr[i].Province)
                {
                    arr[i].Show();
                }
            }
        }
        static void CityShow(Student[] arr, string city)
        {
            for (int i = ; i < arr.Length; i++)
            {
                if (city == arr[i].City)
                {
                    arr[i].Show();
                }
            }
        }
           
/*
        9、编写一个立体几何类SolidGeometry,里面有一个求体积GetVolume的方法,有一个求两个立体几何距离的方法
           编写一个Sphere类,字段包含:半径,x坐标,y坐标,z坐标
           1.求一个球体的体积
           2.判断两个球是否相离
       */
        public static void Task09()
        {
            Sphere s = new Sphere(, , , );
            s.GetVolume(s.Radius);
            Sphere q = new Sphere(, , , );
            s.GetInfo(s, q);
        }
           
/*
        10.创建一个玩家类,
           字段:类型、名称、生命值、魔法值、攻击力、生存状态
           初始化:生命值、魔法值、攻击力的初值分别为800、100、50;
           玩家类有一个攻击方法:
           public void Attack(Player player)。
           测试要求:
           1.产生两个玩家对象:野蛮人和魔法师(用类型区分)
           2.野蛮人每次攻击造成的伤害在[攻击力-10] 到[攻击力+10]之间(这个伤害值是一个随机值),
           3.野蛮人在攻击时有25%的几率产生1次暴击,每次暴击产生的伤害是原来的3倍;
           4.魔法师每次攻击造成的伤害在攻击力的80%~100%之间(也是一个随机数),魔法师每次攻击消耗18点魔法,它会额外减少对方12%的生命值。
           5.让两个对象进行PK,就是你打我一下,我打你一下,直到有一方死亡为止;野蛮人先攻击。
       */
        public static void Task10()
        {
            Player player = new Player("亚瑟", "野蛮人", , , , "正常");
            Player enemy = new Player("虞姬", "法师", , , , "正常");
            Random random = new Random();
            int count = ;
            while (true)
            {
                Console.WriteLine("------------第{0}回合---------------", count);
                player.PlayerAttack(enemy, random.Next(, ));
                enemy.ToString1();
                if (enemy._Hp <= )
                {
                    Console.WriteLine("虞姬牺牲了");
                    break;
                }
                else
                {
                    Console.WriteLine("虞姬还有{0}血", enemy._Hp);
                }
                Console.WriteLine("***************");
                enemy.EnemyAttack(player);
                player.ToString2();
                if (player._Hp <= )
                {
                    Console.WriteLine("亚瑟玩完了");
                    break;
                }
                else
                {
                    Console.WriteLine("亚瑟还有{0}血", player._Hp);
                }
                count++;
                Console.WriteLine();
                Console.WriteLine();
            }