C# 如何打印等腰三角形
我正在努力自学编程,我正在读一本书,书中有一个我无法解决的问题: 允许用户输入两个值:用于打印等腰三角形的字符和三角形的峰值大小。例如,如果用户为字符输入6,为峰值输入6,则应生成以下显示: 这是我目前掌握的代码:C# 如何打印等腰三角形,c#,loops,geometry,C#,Loops,Geometry,我正在努力自学编程,我正在读一本书,书中有一个我无法解决的问题: 允许用户输入两个值:用于打印等腰三角形的字符和三角形的峰值大小。例如,如果用户为字符输入6,为峰值输入6,则应生成以下显示: 这是我目前掌握的代码: char character; int peak; InputValues(out character, out peak); for (int row = 1; row < peak * 2; row++)
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
提前感谢。关闭,但您希望在“后退”时开始减少。 你可以做两个循环;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();
}
与尤里·法克托洛维奇的回答略有不同,我从未使用过跳台,所以我无法抗拒 警告未测试
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));
}
这是一个带循环的版本和一个带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
我没有注意获取用户输入,而是对其进行了硬编码。但如果我没有理解错,这就是你想要的。如果你想一想,这是非常简单的:这使用了一种叫做“递归”的策略,如果你还没有学会,你应该学习它
希望这有帮助。考虑到我的工作方式:
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--));
}
}
+1很好,但是为什么不使用新的stringcharacter peak-Math.Absrow-peak编写这行代码,然后删除第二行呢?forWas试图让它尽可能接近他的代码,以关注代码的逻辑,而不是语法
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));
}
}
}
}
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--));
}
}
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--;
}
}
}
}