Making pyramid using c#

后端 未结 13 2070
花落未央
花落未央 2021-01-03 11:04

My question is how to make a pyramid using * and \'space\' in C#? The output will be like this.

     *
    * *
   * * *
  * * * *
 * * * * *
<
相关标签:
13条回答
  • 2021-01-03 11:29

    Try to think of it as a grid or a matrix and see where you want the '*' in each row and how it relates to your loop index.

    0 讨论(0)
  • 2021-01-03 11:31

    I know it's javascript but might help

    let n = 6;
    for (let i = 1; i <= n; i++) {
      let spaces = n-i;
      let str = '';
      for (let j=0; j < spaces; j++) {
        str+=' ';
      }
      
      for (let j=0; j < i; j++) {
        str+='* ';
      }
      console.log(str)
    }
    
    
    0 讨论(0)
  • 2021-01-03 11:33
    class Program
    
    {
    
        static void Main(string[] args)
    
        {
            int num;
            Console.WriteLine("enter level");
            num = Int32.Parse(Console.ReadLine());
            int count = 1;
    
            for (int lines = num; lines >= 1; lines--)
            {
    
                for (int spaces = lines - 1; spaces >= 1; spaces--)
                {
                    Console.Write(" ");
    
                }
                for (int star = 1; star <= count; star++)
                {
                    Console.Write("*");
                    Console.Write(" ");
    
                }
                count++;
    
                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
    
    0 讨论(0)
  • 2021-01-03 11:33

    The following code might help:

    public static void print(int no)
    {
        for (int i = 1; i <= no; i++)
        {
            for (int j = i; j <= no; j++)
            {
                Console.Write(" ");
            }
            for (int k = 1; k < i * 2; k++)
            {
                if(k % 2 != 0)
                {
                  Console.Write("*");
                }
                else
                {
                  Console.Write(" ");
                } 
            }
            Console.WriteLine();
        } 
         Console.ReadLine();
    }
    
    0 讨论(0)
  • 2021-01-03 11:37

    sorry I missed this was homework... will give a strategy ... instead

    it helps if you do it in notepad and think about what you are doing... you will start to understand the relationship between the line you are on and the spaces and what not...

    0 讨论(0)
  • 2021-01-03 11:37
    using System;               
    using System.Collections.Generic;               
    using System.Linq;
    
    using System.Text;
    
    namespace pyramid_star
    
    {
    
        class Program
        {
            static void Main(string[] args)
            {
                Console.WriteLine("enter a number:");
                int n = Convert.ToInt32(Console.ReadLine());
                for (int i = 1; i <= n; i++)
                {
                    for (int x = i; x <= n; x++)
                    {
                        Console.Write(" ");
                    }
                    for (int j = 1; j <= i; j++)
                    {
                        Console.Write("*"+" ");
                    }
                    Console.WriteLine();
                }
                Console.ReadLine();
            }
        }
    }
    
    0 讨论(0)
提交回复
热议问题