C#题型补充

让用户输入一个奇数,打印菱形,最长的行内容个数为用户输入的个数,并且由英文字母拼接而成

Console.Write("请输入一个数字:");
            try
            {
                int a = Convert.ToInt32(Console.ReadLine());
                if (a % 2 != 0)
                {
                    for (int i = 1; i <= (a+1)/2; i++)//菱形上半部分
                    {
                        int c = ((i * 2 - 1) + 1) / 2 - 1;//中间值
                        string end = "";
                        char  b='a';
                        int count = 0;
                        bool d = false;
                        for (int j = 1; j <=(a+1)/2-i; j++)
                        {
                            end += " ";
                        }
                        for (int j = 1; j <= i * 2 - 1; j++)
                        {
                            end += b;
                            if (count == c)
                            {
                                d = true;
                            }
                            if (d)
                            {
                                if (b == 'A')
                                {
                                    b = 'Z';
                                }
                                else
                                {
                                    b--;
                                }
                            }
                            else
                            {
                                if (b == 'Z')
                                {
                                    b = 'A';
                                }
                                else
                                {
                                    b++;
                                }
                                count++;
                            }
                        }
                        Console.WriteLine(end);
                    }
                    for (int i = 1; i < (a + 1) / 2; i++)//菱形的下半部分
                    {
                        char b = 'a';
                        int c = ((a - i * 2)+1) / 2 - 1;
                        string end = "";
                        int count = 0;
                        bool d = false;
                        for (int j = 1; j <= i; j++)
                        {
                            end += " ";
                        }
                        for (int j = 1; j < (a - i * 2) + 1; j++)
                        {
                            end += b;
                            if (count == c)
                            {
                                d = true;
                            }
                            if (d)
                            {
                                if (b == 'A')
                                {
                                    b = 'Z';
                                }
                                else
                                {
                                    b--;
                                }
                            }
                            else
                            {
                                if (b == 'Z')
                                {
                                    b = 'A';
                                }
                                else
                                {
                                    b++;
                                }
                                count++;

                            }
                        }
                        Console.WriteLine(end);
                    }
                }
                else
                {
                    Console.Write("输入错误");
                }
            }
            catch
            {
                Console.Write("输入错误");
            }

            Console.ReadLine();

 


 

用户输入一个字母,如D,那么就打印 ABCDCBA

//用户输入一个字母,如D,那么就打印 ABCDCBA
            Console.Write("请输入一个字母");
            char a = Convert.ToChar(Console.ReadLine());
            char b = 'a';
            bool c = true;//判断b++或是b--
            //拼接要打印的内容
            for (int i = 1; i <= 26; i++)
            {
                Console.Write(b);
                if (b == a)
                {
                   c= false;
                }
                if (c)
                {
                    b++;
                }
                else
                {
                    b--;
                    if (b == 'a' || b == 'A')
                    {
                        Console.Write(b);
                        break;
                    }
                }

            }
           
                Console.ReadLine();

 

posted on 2016-06-02 23:59  。。小兵  阅读(212)  评论(0编辑  收藏  举报