Go語言程式列印直角三角形星號圖案
在本教程中,我們將編寫一個 Go 語言程式碼來列印直角三角形星號圖案。我們將演示如何在 Go 語言中列印星號圖案。
* * * * * * * * * *
如何列印星號圖案?
上面顯示了一個直角三角形星號圖案,在這個圖案中,您可以看到隨著行數的增加,星號也在增加。圖案是這樣的:第一行 1 個星號,第二行 2 個星號,依此類推。
For loop
for [condition | ( init; condition; increment) | Range] {
statement(s);
}
示例 1:Go 語言程式列印直角星號圖案
演算法
步驟 1 - 匯入 fmt 包。
步驟 2 - 開始函式 main()。
步驟 3 - 宣告並初始化整型變數。
步驟 4 - 使用第一個 for 迴圈遍歷從 1 到“行數”的行。
步驟 5 - 使用第二個 for 迴圈遍歷從 1 到 i 的列,以列印星號圖案。
步驟 5 - 列印完一行中的所有列後,換行,即列印新行。
示例
以下程式碼編譯並執行了列印直角三角形星號圖案的 Go 語言程式:
//GOLANG PROGRAM TO PRINT RIGHT ANGLED STAR PATTERN package main // fmt package provides the function to print anything import "fmt" // calling the main function func main() { // declaring variables with integer datatype to store respective values // i and j are used to iterate through for loop // row variable will contain the total number of rows that a star pattern should contain. var i, j, row int // assigning a value of 5 to the number of rows that the pattern should print. // assuming that value to be 5 row = 5 // Printing the star pattern fmt.Println("\nRight Angled Star Pattern") // for loop to print the number of rows rows for i = 1; i <= row; i++ { // for loop to print the number of columns for j = 1; j <= i; j++ { // printing star(*) symbol using fmt.Print() function fmt.Print("* ") } // printing a new line using fmt.Println() function fmt.Println() } }
輸出
Right Angled Star Pattern * * * * * * * * * * * * * * *
程式碼描述
在上面的程式碼中,我們首先匯入主包和 fmt 包,fmt 包允許我們在螢幕上列印任何內容。
然後開始 main() 函式。
宣告並初始化資料型別為 int 的 i、j 和 row 變數。
讓我們將行數賦值為 5,即為了本示例的目的,讓我們列印一個包含 5 行的星號圖案。
i 和 j 用於遍歷 for 迴圈。它們將選擇用於列印“*”字元的相應行和列。
要列印這種圖案,我們需要在一個 for 迴圈中使用另一個 for 迴圈。第一個 for 迴圈用於選擇行,它從 1 迭代到 row 變數包含的行數。
第二個 for 迴圈從 j = 1 到當前行的值開始。當前行的值是 i 變數的值。
使用 fmt.Print() 函式在第二個 for 迴圈內列印“*”字元。
對於第一行,第二個 for 迴圈不會開始,並且將列印一個“*”字元。
對於第二行,i = 2,在這種情況下,第二個 for 迴圈將從 j = 1 迭代到 j = 2,這樣在第二行將列印兩個“*”字元。
以此類推,直角三角形圖案將被打印出來。
示例 2:使用遞迴列印直角三角形星號圖案的 Go 語言程式
演算法
步驟 1 - 匯入 fmt 包
步驟 2 - 定義 printSpace() 函式。
步驟 3 - 定義 printRow() 函式以遞迴列印星號圖案的行。
步驟 4 - 定義 Star() 函式以遞迴列印新行。
步驟 5 - 宣告並初始化整型變數。
步驟 6 - 呼叫 printRow() 函式。
示例
//GOLANG PROGRAM TO PRINT RIGHT ANGLED STAR PATTERN using recursion package main // fmt package provides the function to print anything import "fmt" // defining printRow() function func printRow(number int) { // selecting rows if number > 0 { // calling the printRow() function recusrsively printRow(number - 1) // printing the star character fmt.Printf("*") } } func star(number int) { if number > 0 { // calling the star function recursively star(number - 1) } // calling the printRow() function printRow(number) // printing new line fmt.Println() } // calling the main function func main() { // declaring variable row var row int = 7 // calling the star() function star(row) }
輸出
* ** *** **** ***** ****** *******
程式碼描述
在上面的程式碼中,我們首先匯入主包和 fmt 包,fmt 包允許我們在螢幕上列印任何內容。
建立兩個名為 printRow() 和 star() 的函式。
printRow() 函式將在選定的行數中列印星號字元。
Star() 函式將列印新行,並將呼叫 printRow() 函式以在選定的行中列印“*”。
然後開始 main() 函式。
初始化資料型別為 int 的 row 變數,並將要列印的星號圖案的行數儲存到其中。
呼叫 star() 函式並將 row 變數作為引數傳遞給它。
結論
我們已成功編譯並執行了一個 Go 語言程式來列印直角星號圖案。
為了列印此星號圖案,我們在這裡使用了遞迴,其中我們遞迴呼叫了兩個函式。
資料結構
網路
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP