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 }
輸出
* *** ***** ******* ******* ***** *** *
方法 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("") } }
輸出
*
* * *
* * * * *
* * * * * * *
* * * * *
* * *
*
廣告
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 語言程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP