2011-02-18 5 views
1

제 질문은 피라미드를 만드는 방법입니다 * 및 '공간'C#에서? 출력은 다음과 같습니다.피라미드 만들기 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

이걸 도와 주려고 열심히 ... 세인트 도움이 대답은 ... lemme 생각 ... – hunter

+10

다른 사람은 그가 숙제에 대한 C#을 사용하는 질투? – hunter

+2

@ 헌터 : 어떻게 그렇게? 어셈블리를 사용할 것으로 기대합니까? –

답변

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 

의 총이있는 경우. 코딩을 맡겨 둘거야.

+0

알겠습니다 ... 지금 프로그램을 수정하겠습니다. 도와 주셔서 감사합니다! – yang

2

문제는 공백이므로 공백을 생각해 보시기 바랍니다. 말해 주시겠습니까? 첫 번째 별 왼쪽의 각 행에 몇 개의 공백이 있습니까? 이것에 대해 생각한다면 자신의 문제를 해결할 수있을 것입니다.

2

격자 또는 행렬로 생각하고 각 행에서 '*'를 원하는 위치와 루프 인덱스와의 관련성을 확인하십시오.

1

미안 난 당신이 관계를 이해하기 시작합니다 ...이 대신

당신이 메모장에서 할 당신이 무엇을하고 있는지에 대해 생각한다면 그것은 도움이 ... 전략을 줄 것이다 ... 숙제였다 놓쳤다 당신이있는 줄과 공백 사이에 무엇이 있는지 ...

+2

그게 숙제 야, 도와 줘도 대답 안해. – hunter

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/etc가 아닌)로 선택했기 때문에 여러분에게 말하고 싶기 때문에 C# 방식으로 문제를 해결해야합니다.

+0

이런 식으로 프로그램을 작성한다면 코딩 논리를 깎을 목적으로 내장 된 방법을 사용하지 마라. –

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

      } 
     } 
    } 
+2

별표 (*)는 어디에 있습니까? –

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

답변을 게시 해 주셔서 감사합니다! 코드 스 니펫이 질문에 대답 할 수있는 동안, 설명과 같이 추가 정보를 추가하는 것이 좋습니다. – j0k

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

      } 
     } 
    } 
} 
+0

이것은 내가 본 이상한 피라미드입니다 .... – Mindless

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

카운트 변수가 일을 단순화합니다. –

1

이 시도하고 C에서이 같은 논리에 따라, C는 PHP, 자바

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