Not quite equilateral but as close as you're likely to get with character graphics. The main things are that you need an odd number of asterisks on each line for centering to work and you need to calculate an offset.
(And, even so, you need output in a monospaced font for this to look right.)
Edit: Some cleanup for readability (and incorporating the change from the first comment).
let treeHeight = 5
let treeWidth = treeHeight * 2 - 1
for lineNumber in 1...treeHeight {
// How many asterisks to print
let stars = 2 * lineNumber - 1
var line = ""
// Half the non-star space
let spaces = (treeWidth - stars) / 2
if spaces > 0 {
line = String(repeating: " ", count: spaces)
}
line += String(repeating: "*", count: stars)
print (line)
}
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…