2011-02-18 91 views
1

我的問題是如何在C#中使用*和'space'來製作金字塔?輸出將是這樣的。使用c製作金字塔#

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

我們只需要在這個程序中使用「for循環」。我只知道如何製作這個。

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

我犯了這樣一個方案:

static void Main(string[]args) 
{ 
int i=o; 
int j=o; 

for(i=5;1>=1;i--) 
    for(j=1;j<=5;j++) 
    { 
    Console.Write("*"); 
    } 
    Console.WriteLine(" "); 
} 

我很困惑,當涉及到金字塔,因爲它包含空格。謝謝你的幫助!

+0

很難幫助這個......莫st幫助就是答案... lemme認爲... – hunter 2011-02-18 02:22:52

+10

其他人嫉妒他使用C#作業嗎? – hunter 2011-02-18 02:23:33

+2

@hunter:怎麼樣?你期待他使用Assembly嗎? – 2011-02-18 02:24:24

回答

12

想想你如何手動打印金字塔。

假設5層深。

1st line: 4 spaces, 1 star, 
2nd line: 3 spaces, star, space, star 
3rd line: 2 spaces, star space star space star 

不要緊,無論你最後一顆星星或不後打印的空間 - 不會對它的外觀差異。

我們看到了什麼?

如果我們有一個總的X水平

line 1: (x-1) spaces, (star space) 
line 2: (x-2) spaces, (star space) twice 
line 3: (x-3) spaces, (star space) three times 
line 4: (x-4) spaces, (star space) four times 

是這樣的格局。我會把編碼留給你。

2

你的問題是空間,所以我建議你考慮空間。告訴我這一點:第一顆星星左側每行有多少個空間?如果你考慮這個問題,你很可能會解決你自己的問題。

2

嘗試將其視爲網格或矩陣,並查看每行中'*'的位置以及它與循環索引的關係。

1

對不起,我錯過了,這是功課......將給予戰略......而不是

它幫助,如果你在記事本中做到這一點,想想你在做什麼?你會開始理解的關係在你所在的線和空間之間,什麼不...

1

3小時後發佈我的答案。我想現在你已經差不多完成了@ iluxa的建議呢?

int height = 20; 
for (int level = 1; level <= height; level++) 
{ 
    string text = string.Join(" ", Enumerable.Repeat("*", level)); 
    Console.WriteLine(text.PadLeft(height - level + text.Length)); 
} 

我使用了一些內置方法,例如, Enumerable.RepeatString.PadLeft,而不是純粹的C語言方式。目的是爲了告訴你,既然你選擇了C#作爲編程語言(而不是C/Java /等),你應該用C#方式解決問題。

3
using System; 

    class Program 
    { 
     static void Main(string[] args) 
     { 
      int num, i, j, k; 
      Console.Write("enter the level:"); 
      num=Convert.ToInt32(Console.ReadLine()); 
      for (i = 1; i <= num; i++) 
      { 
       for (j = 1; j < num-i+1; j++) 
       { 
        Console.Write(" "); 
       } 
       for (k = 1; k <= i; k++) 
       { 
        Console.Write(i); 
        Console.Write(" "); 
       } 
       Console.WriteLine(); 

      } 
     } 
    } 
0
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(); 
     } 
    } 
} 
1
using System; 
using System.Collections.Generic; 
using System.Text; 

namespace Star_Pyramid 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      Program o = new Program(); 
      o.show(); 

      Console.ReadKey(); 
     } 
     void show() 
     { 
      for (int i = 1; i <= 12; i++) 
      { 
       for (int j = 1; j <= 9 - i/2; j++) 
       { 
        Console.Write(" "); 
       } 
       for (int k = 1; k <= i; k++) 
       { 
        Console.Write(" * "); 
        Console.Write(" "); 


       } 
       Console.WriteLine(); 

      } 
     } 
    } 
} 
1
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(); 
    } 
} 
1

試試這個,遵循同樣的邏輯在C,C++,PHP,JAVA

using System; 

class pyramid { 

     static void Main() { 

      /** Pyramid stars Looking down 
       Comment this if u need only a upside pyramid **/ 

      int row, i, j; 

      // Total number of rows 
      // You can get this as users input 
      //row = Int32.Parse(Console.ReadLine()); 
      row = 5;    

      // To print odd number count stars use a temp variable 
      int temp; 
      temp = row; 

      // Number of rows to print 
      // The number of row here is 'row' 
      // You can change this as users input 
      for (j = 1 ; j <= row ; j++) { 

      // Printing odd numbers of stars to get 
      // Number of stars that you want to print with respect to the value of "i"?   
       for (i = 1 ; i <= 2*temp - 1 ; i++) 
        Console.Write("*"); 

      // New line after printing a row    
       Console.Write("\n"); 
       for (i = 1 ; i <= j ; i++)     
        Console.Write(" "); 

      // Reduce temp value to reduce stars count     
       temp--; 
      } 

      /** Pyramid stars Looking up 
       Comment this if u need only a downside pyramid **/ 
      int rowx, k, l; 

      // Total number of rows 
      // You can get this as users input 
      // rowx = Int32.Parse(Console.ReadLine()); 
      rowx = 5; 

      // To print odd number count stars use a temp variable 
      int tempx; 
      tempx = rowx; 

      //Remove this if u use 
      Console.Write("\n"); 

      // Number of rows to print 
      // The number of row here is 'rowx' 

      for (l = 1 ; l <= rowx ; l++) { 

      // Leaving spaces with respect to row 
       for (k = 1 ; k < tempx ; k++) 
        Console.Write(" "); 

      // Reduce tempx value to reduce space(" ") count 
       tempx--; 

      // Printing stars 
       for (k = 1 ; k <= 2*l - 1 ; k++) 
        Console.Write("*"); 

      // New line after printing a row 
       Console.Write("\n"); 
      }   
     } 
} 
1

下面的代碼可能會有所幫助:

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(); 
}