用于打印数字三角形的C#程序?

时间:2015-01-13 06:54:45

标签: c#

class Triangle
{
    static void Main(string[] args)
    {
        int i,j,k,odd=1,size,s=0;
        Console.Write("Enter the Size:");
        size = Convert.ToInt32(Console.ReadLine());
        int nofSpaces=size-1;
        for (i = 1; i <= size; i++)
        {
            for (k = 1; k <= nofSpaces; k++)
            {
                Console.Write(" ");
            }
            for (j = 1; j <= odd; j++)
            {
                if (i >= j)
                {
                    s = s + 1;
                }
                else
                {
                    s = s - 1;
                }

                Console.Write(s);
            }
            Console.Write("\n");
            odd = odd + 2;
            nofSpaces = nofSpaces - 1;
        }
        Console.ReadKey();
    }
}

这是代码,它给出了以下结果:

    1
   232
  34543
 4567654
56789875

但我需要这样的结果:

    1
   121
  12321
 1234321
---------

非常感谢任何帮助。谢谢。

4 个答案:

答案 0 :(得分:2)

添加

s = 0;

在代码的右侧。

答案 1 :(得分:2)

class Triangle
    {
        static void Main(string[] args)
        {
            int i,j,k,odd=1,size;
            Console.Write("Enter the Size:");
            size = Convert.ToInt32(Console.ReadLine());
            int nofSpaces=size-1;
            int s = 0;
            for (i = 1; i <= size; i++)
            {
               int g = 0;
                for (k = 1; k <= nofSpaces; k++)
                {
                    Console.Write(" ");
                }
                for (j = 1; j <= odd; j++)
               {
                    
                   if (i >= j)
                   {


                       Console.Write(j);
                       g = j;
                   }

                   else
                   {

                       //for (int n = j-1; n >= i; n--)
                       //{
                       //    Console.Write(n - 1);
                       //}
                       Console.Write(--g);
                   }
                   
                }
                Console.Write("\n");
                odd = odd + 2;
                nofSpaces = nofSpaces - 1;
            }
            Console.ReadKey();
        }
    }
}

谢谢大家!......上面的代码正在运行....

答案 2 :(得分:1)

你应该试试这个...... 这是for循环的一个简单示例

class Triangle
{
    static void Main(string[] args)
    {
                int size;
                Console.Write("Enter the Size:");
                size = Convert.ToInt32(Console.ReadLine());
                for (int i = 0; i < size; i++)
                {
                    for (int j = size ; j > i; j--)
                    {
                        Console.Write(" ");
                    }

                    for (int x = 1; x <= i; x++)
                    {
                        Console.Write(x);
                    }
                    for (int j = i-1; j > 0; j--)
                    {
                        Console.Write(j);
                    }
                    Console.WriteLine();
                }
                   Console.ReadKey();
        }
}

答案 3 :(得分:0)

请写如下

   static void Main(string[] args)
        {
            int i, j, k, odd = 1, size, s = 0;
            Console.Write("Enter the Size:");
            size = Convert.ToInt32(Console.ReadLine());
            int nofSpaces = size - 1;
            for (i = 1; i <= size; i++)
            {
                for (k = 1; k <= nofSpaces; k++)
                {
                    Console.Write(" ");
                }
                s = 0;
                for (j = 1; j <= odd; j++)
                {
                    if (i >= j)
                    {
                        s = s + 1;
                    }
                    else
                    {
                        s = s - 1;
                    }

                    Console.Write(s);
                }
                Console.Write("\n");
                odd = odd + 2;
                nofSpaces = nofSpaces - 1;
            }
            Console.ReadKey();
        }
相关问题