试图输出等腰三角形。我一直在拿一个正确的三角形

时间:2015-09-28 16:51:00

标签: c# loops

using System;


namespace Shapes
{
    class Program
    {
        static void Main(string[] args)
        {
            Console.WriteLine("Character: ");
            string symbol = (Console.ReadLine());
            Console.WriteLine("Peak of Triangle: ");
            int peak = Int32.Parse(Console.ReadLine());


            int i = 0;
            while (i <= peak) // spaces for triangle
            {

                Console.WriteLine(" ");



                int z = 1;

                while (z <= i) // Symbols for triangle
                {
                    Console.Write(symbol);

                    z++;

                }


                i++;
            }

            Console.ReadLine();
        }
    }
}

输出:应从一开始(*)然后增加,直到达到峰值。然后应该减少到任何输入之一。

我得到的输出并没有减少它在峰值输入处停止

1 个答案:

答案 0 :(得分:1)

您必须将计数器i反转以重新计数为0.

Console.WriteLine("Character: ");
string symbol = (Console.ReadLine());
Console.WriteLine("Peak of Triangle: ");
int peak = Int32.Parse(Console.ReadLine()); // spaces for triangle
int i = 0;
int n = 1;
while (i != -1) // do it until i is negative
{
    Console.WriteLine(" ");

    int z = 1;

    while (z <= i) // Symbols for triangle
    {
        Console.Write(symbol);
        z++;
    }

    i += n; // increments when n = 1. decrements when n = -1

    if (i >= peak) // reverse counter when it reaches peak
    {
        n = -1; 
    }
}