2011-01-13 101 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--; 
      }    
     } 
    } 
} 
相关问题