Swift 程式列印沙漏星形圖案
本教程將討論如何編寫 Swift 程式來列印沙漏星形圖案。
星形圖案是由“*”組成的序列,用於開發不同的圖案或形狀,例如金字塔、矩形、十字形等。這些星形圖案通常用於理解或練習程式流程控制,它們也有助於邏輯思維。
要建立沙漏星形圖案,我們可以使用以下任何一種方法:
使用巢狀 for 迴圈
使用 init() 函式
使用 stride 函式
在這裡,我們將沙漏圖案分成兩部分:上半部分和下半部分來解決問題。
以下是演示:
輸入
假設我們的給定輸入為:
Num = 4
輸出
期望的輸出將是:
* * * * * * * * * * * *
方法 1 - 使用巢狀 For 迴圈
我們可以使用巢狀 for 迴圈建立沙漏圖案或任何其他圖案。這裡每個 for 迴圈處理不同的任務,例如儲存行、列、空格等。
演算法
以下是演算法:
步驟 1 - 宣告變數以儲存三角形圖案的長度。
步驟 2 - 執行一個從 1 到 num-1 的外部 for 迴圈。此迴圈處理要列印的行總數,並且每行都以新行開頭。
步驟 3 - 執行一個從 1 到 i 的巢狀 for 迴圈。此迴圈用於列印空格,並且在每次迭代中空格都會增加一個。
步驟 4 - 執行另一個從 1 到 num-i 的巢狀 for 迴圈。此迴圈用於列印上半部分的沙漏圖案。
步驟 5 - 再次執行一個從 1 到 num-1 的外部 for 迴圈
步驟 6 - 執行一個從 1 到 num-i 的巢狀 for 迴圈。此迴圈用於列印空格,並且在每次迭代中空格都會減少一個。
步驟 7 - 執行另一個從 1 到 i 的巢狀 for 迴圈。此迴圈用於列印下半部分的沙漏圖案。
示例
以下程式展示瞭如何使用巢狀 for 迴圈列印沙漏圖案。
import Foundation import Glibc // Length of the triangle pattern let num = 8 // Outer for loop is used to handle the total // number of rows in upper half portion 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 // upper half sandglass for _ in 1...num-i{ print("*", terminator: " ") } // Add new line print("") } // Outer for loop is used to handle the total // number of rows in lower half portion for i in 1..<num{ // Nested for loop is used to print white // spaces for _ in 1...(num-i){ print(terminator: " ") } // Nested for loop is used to print // lower half sand glass for _ in 1...i{ print("*", terminator: " ") } // Add new line print("") }
輸出
* * * * * * *
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
* * * * * * *
方法 2 - 使用 init() 函式
Swift 提供了一個名為 String.init() 的內建函式。使用此函式,我們可以建立任何圖案。String.init() 函式建立一個字串,其中給定字元會重複指定的次數。
語法
以下是語法:
String.init(repeating:Character, count: Int)
這裡,repeating 表示此方法重複的字元,count 表示給定字元在結果字串中重複的總次數。
演算法
以下是演算法:
步驟 1 - 宣告變數以儲存三角形圖案的長度。
步驟 2 - 執行一個條件為 x>=1 的 while 迴圈。此迴圈使用 init() 函式列印沙漏圖案的上半部分:
while x >= 1{
print(String.init(repeating: " ", count: num-x) + String.init(repeating: "*", count: 2*x-1))
x -= 1
}
這裡,String.init(repeating: " ", count: num-x) 列印空格,而 String.init(repeating: "*", count: 2*x-1) 列印上半部分的沙漏圖案。
步驟 3 - 執行另一個從 1 到 num 的 for 迴圈。此迴圈使用 init() 函式列印沙漏圖案的下半部分:
for y in 1...num{
print(String.init(repeating: " ", count: num-y) + String.init(repeating: "*", count: 2*y-1))
}
這裡,String.init(repeating: " ", count: num-y) 列印空格,而 String.init(repeating: "*", count: 2*y-1) 列印下半部分的沙漏圖案。
示例
以下程式展示瞭如何使用 string.init() 函式列印沙漏圖案。
import Foundation import Glibc // Length of the triangle pattern var num = 4 var x = num // Creating upper half portion // Using String.init() function while x >= 1{ print(String.init(repeating: " ", count: num-x) + String.init(repeating: "*", count: 2*x-1)) x -= 1 } // Creating lower half portion // Using String.init() function for y in 1...num{ print(String.init(repeating: " ", count: num-y) + String.init(repeating: "*", count: 2*y-1)) }
輸出
******* ***** *** * * *** ***** *******
方法 3 - 使用 stride 函式
Swift 提供了一個名為 stride() 的內建函式。stride() 函式用於從一個值移動到另一個值,並進行遞增或遞減。或者我們可以說 stride() 函式從起始值返回一個序列,但不包括結束值,並且給定序列中的每個值都以給定量遞增。
語法
以下是語法:
stride(from:startValue, to: endValue, by:count)
這裡,
from - 表示給定序列使用的起始值。
to - 表示限制給定序列的結束值
by - 表示每次迭代的步長,這裡正值表示向上迭代或遞增,負值表示向下迭代或遞減。
示例
以下程式展示瞭如何使用 stride() 函式列印沙漏星形圖案。
import Foundation import Glibc // Length of the triangle pattern var num = 6 // Creating upper half portion // Handle length of the upper half portion for x in 1...num{ // Print spaces for _ in 1...x{ print(terminator: " ") } // Print "*" for _ in stride(from: x, to: num+1, by: 1){ print("*",terminator : " ") } // Add new lines print("") } // Creating lower half portion // Handle length of the lower half portion for x in 1...num{ // Print spaces for _ in stride(from: num+1, to: x, by: -1){ print(terminator : " ") } // Print "*" for _ in 1...x{ print("*",terminator : " ") } // Add new line print(" ") }
輸出
* * * * * *
* * * * *
* * * *
* * *
* *
*
*
* *
* * *
* * * *
* * * * *
* * * * * *
資料結構
網路
關係型資料庫管理系統
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP