字母三角形 急 期中考试了 (o^^o)

img


                Console.Write("请输入行数和起始字母");
                string str = "ABCDEFGHIJKLMNOPQRSTUVWXYZ";
                string inp = Console.ReadLine().ToUpper();
                int lines = Convert.ToInt32(Regex.Match(inp, @"\d+").Value);
                string letter = Regex.Match(inp, "[A-Z]").Value;
                for (int i = 0; i < lines; i++)
                {
                    string ot = letter;
                    for (int j = 0; j < i; j++)
                    {
                        string add = str.Substring((str.IndexOf(letter) + j + 1) % str.Length, 1);
                        ot = add + ot + add;
                    }
                    Console.WriteLine(ot.PadLeft(lines + i, ' '));
                }
                Console.ReadKey();

img

img


static void Main(string[] args)
        {   
            showNpoint(25);
            Console.ReadKey();
        }

        private static void showNpoint(int count)
        {
            Random r = new Random();

            string[] strE = { "A","B","C","D","E","F","G","H","I","J","K","L","M","N","O","P","Q","R","S","T","U","V","W","X","Y","Z"};

            for (int i = 1; i <=count; i++)
            {
                for (int j = 1; j <=count-i; j++)
                {
                    Console.Write(" ");
                }

                for (int k = 1; k <= 2*i-1; k++)
                {
                    Console.Write(strE[r.Next(0,25)]);
                }
                Console.WriteLine();
            }
        }

img

第 8 行的 BAZYZAE 应该是错的,应为 BAZYZAB
所以 通不过是正常的