2016-04-15 56 views
0

需要一些作業幫助與數字c#三角形嵌套

開發分別顯示兩個(2)以下的圖案,一個在另一個之下一個C#控制檯應用程序。使用循環(提示:嵌套)來生成模式。所有的星號應該以Console.Write(「*」)形式的單個語句顯示。其中顯示星號達到示例中顯示的數字值。一個形式爲Console.WriteLine()的語句;可以用於移動到下一行。記下每個數字的順序。請記住,這是用於生成這兩種模式的兩組獨立循環。你將需要推導出數字是如何計算的(它們是計算的結果)以及將計算放置在循環結構中的位置。您可能無法將顯示的數字硬編碼到您的循環中。

試圖得到以下模式,但我的代碼得到我正確的數字和間距,但不是最後數量的星星數量。我需要結合我的寫作聲明嗎?我應該有一個使用計數器整數的公式)任何幫助表示讚賞。

*2 
**4 
***6 
****8 
*****10 
******12 
*******14 
********16 
*********18 
**********20 

**********20 
*********18 
********16 
*******14 
******12 
*****10 
****8 
***6 
**4 
*2 

using System; 
{ 
    public class Program 
    { 
     const string STAR = "*"; 
     const string SPACE = " "; 
     const int COUNTER = 10; 

     static void Main(string[] args) 
     { 
      firsthalf(); 

      Console.ReadLine(); 
     } 
     static public void firsthalf() 
     { 
      for (int r = 0; r < COUNTER; r++) 
      { 
       for (int c = 0; c <= r; c++) 
       { 
        Console.Write(STAR); 
        Console.Write("{0}", (r + 1) * 2); 
       } 
       Console.WriteLine(); 
      } 
     } 
    } 
} 

回答

0

將打印語句數Console.Write("{0}", (r + 1) * 2);第二循環

for (int r = 0; r < COUNTER; r++) 
{ 
    for (int c = 0; c <= r; c++) 
      Console.Write(STAR);  

    Console.WriteLine("{0}", (r + 1) * 2); 
} 
0
class Program 
{ 
    static void Main(string[] args) 
    { 
     f(1, 5, ascending: true); 
     f(1, 10, ascending: false); 
     Console.ReadLine(); 
    } 

    private static void f(int start, int count, bool ascending) 
    { 
     var indices = Enumerable.Range(start, count); 
     if (!ascending) indices = indices.Reverse(); 
     foreach (int index in indices) 
     { 
      for (int i = 1; i <= index; i++) 
      { 
       Console.Write("*"); 
      } 
      Console.WriteLine(2 * index); 
     } 
    } 
} 
0
using System; 

namespace SOFAcrobatics 
{ 
    public static class Launcher 
    { 
     public static void Main() 
     { 
      Int32 n = 0; 
      Boolean locker = false; 
      for (Int32 i = 2 ; i <= 38 ; i+= 2) 
      { 
       n = ((i > 20) ? (40 - i) : i); 
       if (n == 20 && !locker) 
       { 
        locker = true; 
        i -= 2; 
       } 
       for (Int32 j = 1; j <= (n/2); j++) 
       { 
        Console.Write('*'); 
       } 
       Console.WriteLine(n); 
      } 
      Console.ReadKey(true); 
     } 
    } 
} 

output

0

結合了幾個答案,我真的很感謝幫助。

using System; 


public class Program 
{ 
const string STAR = "*"; 
const string SPACE = " "; 
const int COUNTER = 10; 

public static void Main(string[] args) 
{ 
    firsthalf(); 
    sechalf(); 

    Console.ReadLine(); 
} 

static public void firsthalf() 
{ 
    for (int r = 0; r < COUNTER; r++) 
    { 
    for (int c = 0; c <= r; c++) 
     Console.Write(STAR); 

    Console.WriteLine("{0}", (r + 1) * 2); 
    } 
    Console.WriteLine(); 
} 


static public void sechalf() 
{ 
    for (int r = COUNTER; r > 0; r--) 
    { 
    for (int c = 0; c < r; c++) 
     Console.Write(STAR); 

    Console.WriteLine("{0}", (r + 0) * 2); 
    //Console.WriteLine(); 
    } 
    Console.WriteLine(); 
} 
}