前提・実現したいこと
C#で下記のような表示結果にさせるにはどのようにすれば良いでしょうか。
5と入力した場合:
*
**
***
****
*****
****
***
**
*
気になる質問をクリップする
クリップした質問は、後からいつでもMYページで確認できます。
またクリップした質問に回答があった際、通知やメールを受け取ることができます。
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
まず、ご自身で試したコードを提示してください。
for()か、while()で簡単にできると思いますが。

回答3件
0
こんなのでいいでしょうか?
5を入力したときだけでなく、5以外を入力したときも同じ結果になるようにしています。
C#
1string[] marks = { 2 "*", 3 "**", 4 "***", 5 "****", 6 "*****", 7 "****", 8 "***", 9 "**", 10 "*" 11 }; 12 13System.Console.WriteLine("5を入力してください"); 14var dummy = System.Console.ReadLine(); 15 16for (int i = 0; i < marks.Length; i++) { 17 System.Console.WriteLine(marks[i]); 18}
投稿2019/05/05 15:00
総合スコア310
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2019/05/20 06:23
こういう回答大好きです。ただ、5以外が入力されたときは何もしないで終了するくらいにはしてあげてもよかったのでは……と思いました。
コメントありがとうございます。
5が入力されたときにこの結果を表示するとだけ書かれていたので、あえてこのような処理にしてみました。入力値によって出力が変わる必要があるのなら質問に書いてってことで…。

0
ベストアンサー
負の数、または整数値でないものを入力すると終了します。
コンソールの描画色や描画位置の変更、タスク、描画と位置計算の分離、列挙子、ローカル関数、演算子のオーバーロードなどの技術を使用しています。
C#
1using System; 2using System.Collections.Generic; 3using System.Threading.Tasks; 4 5namespace ConsoleApp1 6{ 7 class Program 8 { 9 static void Main(string[] args) 10 { 11 while (true) 12 { 13 Console.ResetColor(); 14 Console.SetCursorPosition(0, 0); 15 Console.Write("数値を入力してください: "); 16 if (!int.TryParse(Console.ReadLine(), out int n)) break; 17 if (n <= 0) break; 18 var route = GetRoute(new Point(0, 1), n); 19 Draw(route).Wait(); 20 } 21 } 22 23 static async Task Draw(IEnumerable<Point> route, int delay = 50) 24 { 25 Console.Clear(); 26 foreach (var p in route) 27 { 28 DrawPoint(p.X, p.Y, ConsoleColor.Green); 29 await Task.Delay(delay); 30 DrawPoint(p.X, p.Y, ConsoleColor.Yellow); 31 } 32 33 void DrawPoint(int x, int y, ConsoleColor color) 34 { 35 Console.SetCursorPosition(x, y); 36 Console.ForegroundColor = color; 37 Console.Write('*'); 38 } 39 } 40 41 static IEnumerable<Point> GetRoute(Point start, int n) 42 { 43 switch (n) 44 { 45 case 0: 46 yield break; 47 case 1: 48 yield return start; 49 break; 50 default: 51 yield return start; 52 var cursor = start; 53 for (int i = 0; i < n * 2 - 2; i++) 54 { 55 cursor += new Point(0, 1); 56 yield return cursor; 57 } 58 for (int i = 0; i < n - 1; i++) 59 { 60 cursor += new Point(1, -1); 61 yield return cursor; 62 } 63 for (int i = 0; i < n - 2; i++) 64 { 65 cursor += new Point(-1, -1); 66 yield return cursor; 67 } 68 foreach (var p in GetRoute(cursor + new Point(0, 1), n - 2)) 69 { 70 yield return p; 71 } 72 break; 73 } 74 } 75 } 76 77 class Point 78 { 79 public Point(int x, int y) 80 { 81 X = x; 82 Y = y; 83 } 84 85 public int X { get; } 86 public int Y { get; } 87 88 public static Point operator +(Point a, Point b) 89 { 90 return new Point(a.X + b.X, a.Y + b.Y); 91 } 92 93 public static Point operator -(Point a, Point b) 94 { 95 return new Point(a.X - b.X, a.Y - b.Y); 96 } 97 } 98} 99
投稿2019/05/05 15:53
編集2019/05/06 03:03総合スコア28675
あなたの回答
tips
太字
斜体
打ち消し線
見出し
引用テキストの挿入
コードの挿入
リンクの挿入
リストの挿入
番号リストの挿入
表の挿入
水平線の挿入
プレビュー
質問の解決につながる回答をしましょう。 サンプルコードなど、より具体的な説明があると質問者の理解の助けになります。 また、読む側のことを考えた、分かりやすい文章を心がけましょう。