Created
December 15, 2016 14:42
-
-
Save MeilCli/cbc4947d379cdbcf88e4cfd38baeafed to your computer and use it in GitHub Desktop.
ピラミッド
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Text; | |
using System.Threading.Tasks; | |
namespace Pyramid { | |
public class Program { | |
public static void Main(string[] args) { | |
Console.OutputEncoding = Encoding.UTF8; | |
Console.Out.WriteLine("行数を入力してください"); | |
string text = Console.In.ReadLine(); | |
int count = 0; | |
if(int.TryParse(text,out count) == false) { | |
Console.Out.WriteLine("数値を入力してください"); | |
return; | |
} | |
new Pyramid(count).WritePyramid(); | |
} | |
} | |
public class Pyramid { | |
private List<PyramidRow> rows = new List<PyramidRow>(); | |
public int RowCount => rows.Count; | |
public Pyramid() { } | |
public Pyramid(int rowCount) { | |
AddRangeRow(rowCount); | |
} | |
public void AddRow() { | |
rows.Add(new PyramidRow(RowCount)); | |
} | |
public void AddRangeRow(int count) { | |
for(int i = 0;i < count;i++) { | |
AddRow(); | |
} | |
} | |
public void RemoveRow() { | |
rows.RemoveAt(RowCount - 1); | |
} | |
public void WritePyramid() { | |
foreach(PyramidRow row in rows) { | |
row.WriteLine(RowCount); | |
} | |
} | |
} | |
public class PyramidRow { | |
private int position; | |
public PyramidRow(int position) { | |
this.position = position; | |
} | |
public void WriteLine(int totalRowCount) { | |
foreach(char c in Enumerable.Repeat(' ',totalRowCount - position - 1)) { | |
Console.Out.Write(c); | |
} | |
foreach(char c in Enumerable.Repeat('*',position * 2 + 1)) { | |
Console.Out.Write(c); | |
} | |
Console.Out.WriteLine(); | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment