2011-01-13 172 views
-1

我想通過自己學習編程,我是從一本書,有以下問題的工作,我解決不了:如何打印等腰三角形

允許用戶輸入的兩個值:用於打印等腰三角形的字符以及三角形的峯值大小。例如,如果用戶輸入#用於顯示峯的字符和6,應產生以下顯示:

##

###

## ##

#####

######

#####

####

###

##

這是迄今爲止我已經得到了代碼:

 char character; 
     int peak; 

     InputValues(out character, out peak); 

     for (int row = 1; row < peak * 2; row++) 
     { 
      for (int col = 1; col <= row; col++) 
      {      
       Console.Write(character); 
      } 
      Console.WriteLine(); 
     } 
     Console.Read() // hold console open 

在此先感謝。

回答

2
for (int row = 0; row < peak; row++) 
{ 
    Console.WriteLine(new string(character, row + 1)); 
} 
for (int row = 1; row < peak; row++) 
{ 
    Console.WriteLine(new string(character, peak - row)); 
} 
2

關閉,但是當你'回落'時你想開始減少。 你可以做兩個循環;
0 -> peak,然後(peak - 1 -> 0),這將打印兩個'方向'。

另一種方法是找出距離峯頂有多遠的行,並打印出很多字符。

for (int row = 0; row < peak*2; row++) 
    { 
     for(var i = 0; i < peak - Math.Abs(row - peak); i++) 
      Console.Write(character); 
     Console.WriteLine(); 
    } 
+0

+ 1不錯,但爲什麼不寫有'新的字符串行(文字,峯 - Math.Abs​​(行峯))`和放棄 – 2011-01-13 02:44:27

1

到尤里Faktorovich的回答輕微替代(我從來沒有使用分步起伏,所以我無法抗拒)

警告沒有測試

for (int row = 0; row < peak; row++) 
{ 
    Console.WriteLine(new string(character, row + 1)); 
} 
for (int row = peak, row > 1; row--) 
{ 
    Console.WriteLine(new string(character, row)); 
} 
0

這裏是一個版本一個循環和LINQ版本...

Console.WriteLine("peak"); 
var peak = int.Parse(Console.ReadLine()); 
Console.WriteLine("char"); 
var @char = (char)Console.Read(); 

//As LINQ 
var query = (from row in Enumerable.Range(0, peak * 2) 
      let count = peak - Math.Abs(peak - row) 
      let str = new string(@char, count) 
      select str 
      ).ToArray(); //if you have .Net 4.0+ you don't need the .ToArray() 
Console.WriteLine(string.Join(Environment.NewLine, query)); 

//As Loop 
for (int r = 1; r < peak * 2; r++) 
    Console.WriteLine("{0}", new string(@char, peak - Math.Abs(peak - r))); 
Console.Read(); // hold console open 
0
using System; 
namespace ConsoleApplication1 
{ 
    class Program 
    { 
     static void Main(string[] args) 
     { 
      char drawChar = '#'; 
      int repeatNumber = 5; 
     Program.drawIsoscelesTraiangle(drawChar, repeatNumber, 1); 
     Console.ReadKey(); 
    } 
    static void drawIsoscelesTraiangle(char repeatChar, int peak, int current) 
    { 
     if (current < peak) 
     { 
      Console.WriteLine(new string(repeatChar, current)); 
      Program.drawIsoscelesTraiangle(repeatChar, peak, current + 1); 
      Console.WriteLine(new string(repeatChar, current)); 
     } 
     else 
     { 
      Console.WriteLine(new string(repeatChar, current)); 
     } 
    } 
} 
} 

我沒有照顧獲取用戶輸入,而是硬編碼它。但是如果我不明白的話,這就是你想要的。這是非常簡單的,如果你給它一個想法:)這使用一種稱爲'遞歸',你應該學習,如果你還沒有,但是。

希望這會有所幫助。問候

1

的方式我會做:

static void drawIsoscelesTraiangle(char repeatChar, int peak, int current) 
    { 
     for (int i = 0; i < peak; i++) 
     { 
      Console.WriteLine(new String(repeatChar, current++)); 
     } 
     for (int i = current; i > 0; i--) 
     { 
      Console.WriteLine(new String(repeatChar, current--)); 
     } 

    } 
0
using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Threading.Tasks; 


namespace Ch6ex10 
{ 
    class Isosceles 
    { 
     static void Main(string[] args) 
     {    
      OutputTraiangle(InputCharacter(), InputPeak()); 
      Console.ReadLine(); 
     } 

     //Allow user to input a character to be used as the triangle 
     public static char InputCharacter() 
     { 
      string inputValue; 
      char character; 

      Console.Write("Enter the character to be used to display the triangle: "); 
      inputValue = Console.ReadLine(); 
      character = Convert.ToChar(inputValue); 
      return character; 
     } 

     //Allow user to input an integer for the peak of the triangle 
     public static int InputPeak() 
     { 
      string inputPeak; 
      int peak; 

      Console.Write("Enter the integer amount for the peak of the triangle: "); 
      inputPeak = Console.ReadLine(); 
      peak = Convert.ToInt32(inputPeak); 
      return peak; 
     } 

     //Output the triangle using the users character choice and peak size (first half) 
     public static void OutputTraiangle(char character, int peak) 
     { 
      int start = 1; 
      int finish = (peak - 1); 

      while (start != peak) 
      { 
       for (int amount = 1; amount <= start; amount++) 
       { 
        Console.Write(character); 
       } 
       Console.WriteLine(); 
       start++;     
      } 

      for (int amount = 1; amount <= peak; amount++) 
       { 
        Console.Write(character); 
       } 
      Console.WriteLine(); 

      while (finish != 0) 
      { 
       for (int amount = 1; amount <= finish; amount++) 
       { 
        Console.Write(character); 
       } 
       Console.WriteLine(); 
       finish--; 
      }    
     } 
    } 
}