2016-04-13 34 views
0

到目前爲止,我已經想通了如何做到這一切,但我需要知道如何使這個代碼以這種方式行爲。C#求和排除

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


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

關鍵是什麼讓它跑回來呢?我假設從5開始挑選2個整數,但我很難追蹤它。這是我迄今爲止要求的兩個以前的解決方案。

//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

爲什麼不直接從張貼在編程過程中充分的問題嗎?我記得15年前在uni做這些入門練習。唉,你已經有人願意爲你做功課了。 – Brad

回答

1

C#,您可以使用它們填充在左側PadLeft到右對齊在這種情況下的字符。

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

輸出:

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

工作Demo

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

我必須爲每一個做一個嵌套循環。我將如何做到這一點,而不是相反? –

0

你可以這樣做:

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

這給了我:

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