C# · 12月 20, 2021

C# — 随机数产生的字母金字塔

C# — 随机数产生的字母金字塔

1. 代码实现:

Main( </span><span style=”color: #0000ff”&gt;private</span> <span style=”color: #0000ff”&gt;static</span> <span style=”color: #0000ff”&gt;void</span> showNpoint(<span style=”color: #0000ff”&gt;int</span><span style=”color: #000000″&gt; count) { Random r </span>= <span style=”color: #0000ff”&gt;new</span><span style=”color: #000000″&gt; Random(); </span><span style=”color: #0000ff”&gt;string</span>[] strE = { <span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;A</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;B</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;C</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;D</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;E</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;F</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;G</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;H</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;I</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;J</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;K</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;L</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;M</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;N</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;O</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;P</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;Q</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;R</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;S</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;T</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;U</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;V</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;W</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;X</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;Y</span><span style=”color: #800000″&gt;”</span>,<span style=”color: #800000″&gt;”</span><span style=”color: #800000″&gt;Z</span><span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;}; </span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> i = <span style=”color: #800080″&gt;1</span>; i <=count; i++<span style=”color: #000000″&gt;) { </span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> j = <span style=”color: #800080″&gt;1</span>; j <=count-i; j++<span style=”color: #000000″&gt;) { Console.Write(</span><span style=”color: #800000″&gt;”</span> <span style=”color: #800000″&gt;”</span><span style=”color: #000000″&gt;); } </span><span style=”color: #0000ff”&gt;for</span> (<span style=”color: #0000ff”&gt;int</span> k = <span style=”color: #800080″&gt;1</span>; k <= <span style=”color: #800080″&gt;2</span>*i-<span style=”color: #800080″&gt;1</span>; k++<span style=”color: #000000″&gt;) { Console.Write(strE[r.Next(</span><span style=”color: #800080″&gt;0</span>,<span style=”color: #800080″&gt;25</span><span style=”color: #000000″&gt;)]); } Console.WriteLine(); } }</span></pre>

2. 运行结果: