2016-04-13 17 views
0

Şimdiye kadar her şeyi nasıl yapacağımı anladım ama bu kodun bu şekilde nasıl davranacağını bilmem gerekiyor.C# Toplam dışlama

*     * 
*     * 
    *    * 
    *    * 
    *   * 
    *   * 
     *  * 
     *  * 
     * * 
     * * 
      * 
//and now the reverse 


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

Anahtar da böyle dışarı çıkmasına neden oluyor? 5'de başlamayı ve 2 tam sayı almayı düşünüyorum ama onu izlemekte zorlanıyorum. Şimdiye kadar talep ettiğim iki çözüm için şimdiye kadar sahip olduğum şey.

//CIS 110 Program 7 
//Online 
using System; 
class Program7 
{ 
    static int count, sum, countmax; 

    static void Main() 
    { 
     GetData(); 
    } 
    static void GetData() 
    { 
     count = 0; 
     sum = 0; 
     countmax = 10; 
     while (count <= countmax) 
     { 
      while (sum <= count) 
      { 
       sum++; 
       Console.Write('*'); 
      } 
      Console.WriteLine(); 
      count++; 
      sum = 0; 
     } 
    } 
} 
//* 
//** 
//*** 
//**** 
//***** 
//****** 
//******* 
//******** 
//********* 
//********** 
//*********** 
//Press any key to continue . . . 

//CIS 110 Program 7 
//Online 
using System; 
class Program7 
{ 
    static int count, sum, countmax; 

    static void Main() 
    { 
     GetData(); 
    } 
    static void GetData() 
    { 
     count = 9; 
     sum = 0; 
     while (count >= 0) 
     { 
      while (sum <= count) 
      { 
       sum++; 
       Console.Write('*'); 
      } 
      Console.WriteLine(); 
      count--; 
      sum = 0; 
     } 

    } 
} 

//********** 
//********* 
//******** 
//******* 
//****** 
//***** 
//**** 
//*** 
//** 
//* 
//Press any key to continue . . . 
+0

Neden sadece programlama dersten tam soru gönderebilir? 15 yıl önce uni'de bu alıştırmaları yaptığımı hatırlıyorum. Ne yazık ki, zaten sizin için ev ödevlerinizi isteyerek yapıyorsunuz. – Brad

cevap

1

C# olarak şunları yapmanız PadLeft sol cenahta doldurma tarafından bu durumda karakterleri sağ hizalar kullanabilirsiniz.

static void GetData() 
{ 
    int count = 0; 
    int sum = 0; 
    int countmax = 15; 
    bool IsOdd = countmax%2 ==1; 

    // first half 
    for(int i=1,j=0;i<=countmax/2;i++) 
    { 
     Console.WriteLine("{0}{1}","*".PadLeft(i), "*".PadLeft(countmax-i-(j++))); 
    }  

    if(IsOdd) Console.WriteLine("*".PadLeft(countmax/2 +1)); // for odd count 

    // Second half 
    for(int i=IsOdd? countmax/2+1: countmax/2, j=0 ;i<countmax;i++) 
    { 
     Console.WriteLine("{0}{1}","*".PadLeft(i-(IsOdd?++j : j++)), "*".PadLeft(IsOdd?++j:j++)); 
    } 
} 

Çıktı:

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

Çalışma Demo Ayrıca basit iç içe döngüler yapabilirsiniz

0

-

public static void Main() 
     { 
      GetData(); 
     } 
     static void GetData() 
     { 
      int count = 11; 
      for (int i = 0; i < count; i++) 
      { 
       for (int j = 0; j < count * 2; j++) 
       { 
        var s = ((i + j == count) || (j - i == count)) ? "*" : " "; 
        Console.Write(s); 
       } 
       Console.WriteLine(); 
      } 

      Console.WriteLine("-------------------------------"); 
      Console.WriteLine("------------Reverse------------"); 
      Console.WriteLine("-------------------------------"); 

      for (int i = count; i >= 0; i--) 
      { 
       for (int j = count * 2; j >= 0; j--) 
       { 
        var s = ((i + j == count) || (j - i == count)) ? "*" : " "; 
        Console.Write(s); 
       } 
       Console.WriteLine(); 
      } 

      Console.ReadLine(); 
     } 
+0

Bunların her biri için yuvalanmış bir döngü yapmak zorundayım. Bunu ters olmadan nasıl yapardım? –

0

Bunu yapabilirsiniz:

bana verir
var count = 7; // number of starts from centre star along one arm of the X 

var range = Enumerable.Range(1, count); 

var lines = 
    range 
     .Concat(range.Reverse().Skip(1)) 
     .Select(n => 
      "*".PadLeft(n, ' ') 
      + (n == count ? "" : "*".PadLeft(2 * (count - n), ' '))); 

Console.WriteLine(String.Join(Environment.NewLine, lines)); 

:

 
*   * 
*   * 
    *  * 
    *  * 
    * * 
    * * 
     * 
    * * 
    * * 
    *  * 
    *  * 
*   * 
*   * 
İlgili konular