Swift 程式列印半菱形數字圖案
本教程將討論如何編寫 Swift 程式以列印半菱形數字圖案。
數字圖案是一系列數字,用於開發不同的圖案或形狀,例如金字塔、矩形、十字架等。這些數字圖案通常用於理解或練習程式流程控制,它們也有利於邏輯思維。
要建立半菱形數字圖案,我們可以使用以下任何一種方法:
使用巢狀 for 迴圈
使用 init() 函式
使用 stride 函式
方法 1 - 使用巢狀 For 迴圈
我們可以使用巢狀 for 迴圈建立半菱形數字圖案或任何其他圖案。
演算法
以下是演算法:
步驟 1 - 宣告變數以儲存半菱形數字圖案的高度
對於上半部分圖案:
步驟 2 - 從 1 到 num 執行外部 for 迴圈。此迴圈處理要列印的行總數,並且每一行都以新行開頭。
步驟 3 - 從 1 到 x 執行巢狀 for 迴圈。此迴圈用於列印上半部分菱形數字圖案。對於下半部分菱形星形圖案:
步驟 4 - 再次從 1 到 num-1 執行外部 for 迴圈。此迴圈處理要列印的行總數,並且每一行都以新行開頭。
步驟 5 - 從 1 到 num-q 執行另一個巢狀 for 迴圈。此迴圈用於列印下半部分菱形數字圖案。
示例
以下程式演示瞭如何使用巢狀 for 迴圈列印半菱形數字圖案。
import Foundation import Glibc // Height of the half diamond numeric pattern let num = 9 // Outer for loop is used to handle the // total number of rows in upper half // diamond numeric pattern for x in 1...num{ // Nested for loop is used to print // upper half diamond numeric pattern for y in 1...x{ print(y, terminator: "") } // Add new line print("") } // Outer for loop is used to handle the // total number of rows in lower half // diamond numeric pattern for p in 1...num-1{ // Nested for loop is used to print // lower half diamond numeric pattern for q in 1...num-p{ print(q, terminator: "") } // Add new line print("") }
輸出
1 12 123 1234 12345 123456 1234567 12345678 123456789 12345678 1234567 123456 12345 1234 123 12 1
方法 2 - 使用 init() 函式
Swift 提供了一個名為 String.init() 的內建函式。使用此函式,我們可以建立任何圖案。String.init() 函式建立一個字串,其中給定字元重複指定次數。
語法
以下是語法:
String.init(repeating:Character, count: Int)
這裡,repeating 表示此方法重複的字元,count 表示給定字元在結果字串中重複的總次數。
演算法
以下是演算法:
步驟 1 - 宣告變數以儲存半菱形星形圖案的長度。
步驟 2 - 從 1 到 num 執行 for 迴圈。此迴圈使用 init() 函式列印上半部分菱形數字圖案
for x in 1...num{
print(String.init(repeating: "22", count: x))
}
這裡,在每次迭代中,init() 函式根據 x 的值列印“22”。例如,如果 x = 2,則 init() 列印“2222”。
步驟 3 - 從 1 到 num-1 執行另一個 for 迴圈。此迴圈使用 init() 函式列印下半部分菱形星形圖案
for y in 1...num-1{
print(String.init(repeating: "33", count: num-y))
}
示例
以下程式演示瞭如何使用 string.init() 函式列印半菱形數字圖案。
import Foundation import Glibc // Height of the half diamond numeric pattern let num = 4 // Creating upper half diamond numeric pattern // Using String.init() function for x in 1...num{ print(String.init(repeating: "22", count: x)) } // Creating lower half diamond numeric pattern // Using String.init() function for y in 1...num-1{ print(String.init(repeating: "33", count: num-y)) }
輸出
22 2222 222222 22222222 333333 3333 33
方法 3 - 使用 stride 函式
Swift 提供了一個名為 stride() 的內建函式。stride() 函式用於從一個值以增量或減量移動到另一個值。或者我們可以說 stride() 函式從起始值返回一個序列,但不包括結束值,並且給定序列中的每個值都以給定量步進。
語法
以下是語法:
stride(from:startValue, to: endValue, by:count)
這裡,
from - 表示要用於給定序列的起始值。
to - 表示限制給定序列的結束值
by - 表示每次迭代要步進的量,這裡正值表示向上迭代或增量,負值表示向下迭代或減量。
示例
以下程式演示瞭如何使用 stride() 函式列印半菱形數字圖案。
import Foundation import Glibc // Height of the half diamond numeric pattern let num = 3 // For upper half diamond numeric pattern for x in 1...num{ // Printing upper half diamond numeric pattern for y in 1...x{ print(y, terminator : "") } // Adding new line print(" ") } // For lower half diamond numeric pattern for m in 1...num-1{ // Printing lower half diamond numeric pattern for n in stride(from: num, to: m, by: -1){ print(n, terminator : "") } // Adding new line print(" ") }
輸出
1 12 123 32 3
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP