Swift 程序打印菱形星号图案
本教程将讨论如何编写 Swift 程序来打印菱形星号图案。
星号图案是一系列“*”,用于开发不同的图案或形状,例如金字塔、矩形、十字形等。这些星号图案通常用于理解或练习程序流程控制,它们也有利于逻辑思维。
要创建菱形星号图案,我们可以使用以下任何一种方法:
使用嵌套 for 循环
使用 init() 函数
使用 stride 函数
在这里,我们将菱形分成两部分:上半部分金字塔和下半部分金字塔来解决问题。
下面是同一问题的演示:
输入
假设我们给定的输入是:
Num = 5
输出
期望的输出将是:
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
方法 1 - 使用嵌套 for 循环
我们可以使用嵌套 for 循环创建菱形星号图案或任何其他图案。这里每个 for 循环处理不同的任务,例如保存行、列、空格等。
示例
以下程序演示了如何使用嵌套 for 循环打印菱形星号图案。
import Foundation import Glibc // Length of the triangle pattern let num = 5 // Upper pyramid // Outer for loop is used to handle the // total number of rows in upper pyramid for i in 1...num{ // Nested for loop is used to print white // spaces for _ in 0..<(num-i){ print(" ", terminator: " ") } // Nested for loop is used to print upper pyramid of "*" for _ in 1...2*i-1{ print("*", terminator: " ") } // Add new line print("") } // lower pyramid // Outer for loop is used to handle the // total number of rows in lower pyramid for i in 1..<num{ // Nested for loop is used to print white // spaces for _ in 1...i{ print(" ", terminator: " ") } // Nested for loop is used to print lower pyramid of "*" for _ in 1...(2*num - 2*i - 1){ print("*", terminator: " ") } // Add new line print("") }
输出
* * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * * *
方法 2 - 使用 init() 函数
Swift 提供了一个名为 String.init() 的内置函数。使用此函数,我们可以创建任何图案。String.init() 函数创建一个字符串,其中给定的字符重复指定的次数。
语法
以下是语法:
String.init(repeating:Character, count: Int)
这里,repeating 表示此方法重复的字符,count 表示给定字符在结果字符串中重复的总次数。
示例
以下程序演示了如何使用 string.init() 函数打印菱形星号图案。
import Foundation import Glibc // Length of the diamond var num = 8 var x = 1 // Handle the rows of the diamond while x <= num{ // For upper half of the diamond // Checking for even length if num%2 == 0{ if x <= num/2{ let mystar = String.init(repeating: "*", count:(x-1)*2+1) let spaceing = String.init(repeating: " ", count:((num/2)-x)) print(spaceing+mystar) } } // Checking for odd length else{ if x < num/2+1{ let mystars = String.init(repeating: "*", count:(x-1)*2+1) let myspace = String.init(repeating: " ", count:((num/2)-x+1)) print(myspace+mystars) } } // For lower half of the diamond if x > num/2{ let star = String.init(repeating: "*", count:(num-x)*2+1) let mspace = String.init(repeating: " ", count:(x-((num/2)+1))) print(mspace+star) } x+=1 }
输出
* *** ***** ******* ******* ***** *** *
Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.
方法 3 - 使用 stride 函数
Swift 提供了一个名为 stride() 的内置函数。stride() 函数用于以增量或减量从一个值移动到另一个值。或者我们可以说 stride() 函数从起始值返回一个序列,但不包括结束值,并且给定序列中的每个值都以给定量递增。
语法
以下是语法:
stride(from:startValue, to: endValue, by:count)
这里,
from − 表示要用于给定序列的起始值。
to − 表示限制给定序列的结束值
by − 表示每次迭代的步长,这里正值表示向上迭代或增量,负值表示向下迭代或减量。
示例
以下程序演示了如何使用 stride() 函数打印菱形星号图案。
import Foundation import Glibc // Length of the diamond var num = 8 // Print the upper half of the diamond for x in 1...num{ if x % 2 != 0{ // Print white spaces for _ in stride(from: num, to: x, by: -1){ print(terminator : " ") } // Print star pattern for _ in 1...x{ print("*", terminator : " ") } print("") } } // Print the lower half of the diamond for x in stride(from: num, to: 1, by: -1){ if x % 2 != 0{ // Print white spaces for _ in stride(from: num, to: x-2, by: -1){ print(terminator : " ") } // Print star pattern for _ in stride(from: 2, to: x, by: 1){ print("*", terminator : " ") } print("") } }
输出
* * * * * * * * * * * * * * * * * * * * * * * * *