使用循环打印星形三角形

时间:2015-10-27 02:43:58

标签: c#

如何打印星形等边三角形,如下图所示?

我试过这个,但没有得到所需的模式

for (int i = 1; i <= number; i++)         
{
    for (int j = 1; j <= number - i; j++)
    {
        Console.Write(" ");
    }
    for (int k = 1; k <= i; k++)
    {
        Console.Write(" *");
        if (k > 3)
        {
            for (int l = 1; l <= k - 2; l++)
            {
                Console.Write(" ");
            }

        }
    }
    Console.WriteLine(" ");
}

     *
   *   *
  *     *
 *       *
* * * * * *

5 个答案:

答案 0 :(得分:1)

试试这个

using System;

namespace ConsoleApplication4
{
    class Program
    {
        static void Main(string[] args)
        {
            int number = 6;
            string toDisplay = "";

            for (int i = 1; i <= number; i++)
            {
                for (int j = 1; j <= number - i; j++) Console.Write(" ");
                for (int k = 1; k <= i; k++)
                {
                    toDisplay = (k == 1 || k == i && i != number)
                                ?" *"
                                : (i == number)
                                ? " *"
                                : "  ";
                    Console.Write(toDisplay);
                }

                Console.WriteLine("");
            }
            Console.ReadLine();
        }
    }
}


//////////////////////////////////
      *
     * *
    *   *
   *     *
  *       *
 * * * * * *


////////////////////////////////////

答案 1 :(得分:1)

这是一种有趣的方式

<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<div id="slidebottom">
   <button>Slide it</button>
   <div id="outer-nav">
     <div class="navlink-1">Link 1</div>
     <div class="navlink-2">Link 2</div>
     <div class="navlink-3">Link 3</div>
   </div>
</div>

输出

using System;
using System.Linq;

public class Program
{
    public static void Main()
    {
        int number = 5;
        for (int i = 0; i < number; i++)
        {
            Console.Write(string.Concat(Enumerable.Repeat(" ", number - i))); // Left Spaces
            Console.Write("*"); // Left side
            if (i != 0) // If not first line 
            {
                Console.Write(string.Concat(Enumerable.Repeat(" ", i * 2 - 1))); // Middle Spaces
                Console.Write("*"); // Right side
            }
            Console.WriteLine();
        }

        for (int i = 0; i <= number; i++)
            Console.Write("* "); // Bottom
    }
}

答案 2 :(得分:1)

一种不同的方法

int number = 5;
var pattern = "*";
for(int i=0;i<number;i++)
{           
    Console.WriteLine(pattern.PadLeft(number-i,' ')
                      +((i==0)?"":(pattern.PadLeft((i)*2,' '))));
}

答案 3 :(得分:1)

    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;

    namespace Triangle
    {
        class Program
        {
            static string Line(int i, int n, string star, string space)
            {
                string rslt = String.Empty.PadRight(n - 1 - i, ' ') + star;
                if (i == 0) return rslt;
                for (int j = 0; j < (i > 0 ? 2 * i - 1 : 0) / 2; j++ rslt += space + (i >= (n - 1) ? star : space)) ;
                rslt += space + star;
                return rslt;
            }

            static void Main(string[] args)
            {
                int n = 40;
                for (int i = 0; i < n; i++)
                {
                    Console.WriteLine(Line(i,n, "*", " "));
                }
                Console.ReadLine();
            }
        }
    }

答案 4 :(得分:0)

使用 Java

例如:

      *
     * *
    *   *
   *     *
  *********

代码:

int numberOfRows = 5;
    
for(int i=0; i<numberOfRows; i++)
{
    for(int a=0; a<=numberOfRows; a++)
    {
        int x = numberOfRows - i;
            
        if (a > x) {
           if (x==(a-1) || i==(numberOfRows-1)) {
               System.out.print("*");
           } else {
               System.out.print(" ");
           }
        } else {
           System.out.print(" ");
        }
    }
                    
    for(int b=0; b<=i; b++)
    {
        if(b==i || i==(numberOfRows-1)) {
            System.out.print("*");
        } else {
            System.out.print(" ");
        }
    }

    System.out.print("\n");
}