用c#程序开发一个金字塔,使用for循环

本文关键字:金字塔 使用 for 循环 一个 程序开发 | 更新日期: 2023-09-27 17:54:32

我可能错过了什么,但我找不到解决这个简单问题的方法。

我应该写一个短代码请求一个正整数,然后使用for语句打印出一个金字塔,f.ex

Height: 0
Height: 4
   *
  ***
 *****
*******

下面是代码本身

using System; 
namespace starpyramid
{ 
    class program 
    { 
        static void Main() 
        { 
            Veryfy:
            Console.Write("Height: "); 
            int i = int.Parse(Console.ReadLine());
            if(i>0)
            {
            goto main;
            }
            else
                {
            goto Verify;
            }   
        main:
            for (int h = 1; h< luku1;h++) // main loop for the lines
                {
                for (int s = 1; s < luku1; s++) //for spaces before the stars
                    {
                        if (s == h)
                        {
                        break;
                        }
                        Console.Write(" ");
                    }
                Console.Write("*'n");
                }
    }   
    } 
}

也许有人能帮我解决这个问题

用c#程序开发一个金字塔,使用for循环

它的工作很完美,

using System; 
namespace starpyramid
{ 
    class program 
    { 
        static void Main() 
        { 
            Console.Write("Height: "); 
            int i = int.Parse(Console.ReadLine());
            if(i>0)
            {
            goto main;
            }
            else
            {
                Main();
            }   
            main:
            Console.Write("'n");
            for (int h = 1; h<= i;h++) // main loop for the lines
            {
                for (int s = h; s <= i; s++) //for spaces before the stars
                {
                    Console.Write(" ");
                }
                for(int j=1; j<(h*2); j=j+2)
                {
                    Console.Write("*");
                }
            Console.Write("'n");
            }
    }   
    } 
}

但没有goto程序也可以工作,参见

using System; 
namespace starpyramid
{ 
    class program 
    { 
        static void Main() 
        { 
            Console.Write("Height: "); 
            int i = int.Parse(Console.ReadLine());
            Console.Write("'n");
            for (int h = 1; h<= i;h++) // main loop for the lines
            {
                for (int s = h; s <= i; s++) //for spaces before the stars
                {
                    Console.Write(" ");
                }
                for(int j=1; j<(h*2); j=j+2)
                {
                    Console.Write("*");
                }
            Console.Write("'n");
            }
    }   
    } 
}