Go語言程式建立金字塔和圖案


在本教程中,我們將學習如何在 Go 程式語言中建立不同圖案的金字塔。

語法

NESTED FOR LOOP
for [condition | ( init; condition; increment ) | Range] {
   for [condition | ( init; condition; increment ) | Range] {
      statement(s);
   }
   statement(s);
}

示例 1:使用 $ 建立金字塔和圖案的 Go 語言程式碼

演算法

  • 步驟 1 − 匯入 fmt 包。

  • 步驟 2 − 啟動函式 main ()

  • 步驟 3 − 宣告並初始化變數。

  • 步驟 4 − 使用帶有條件和增量的 for 迴圈。

  • 步驟 5 − 使用 fmt.Println () 列印結果。

示例

package main // fmt package provides the function to print anything import "fmt" func main() { // declare the integer variables var i, j, rows int rows = 7 // Scanln() reads data from rows and stores it in the places // specified by the additional arguments in the parameter format fmt.Scanln(&rows) fmt.Println("\nGolang program to create pyramid and pattern") for i = 1; i <= rows; i++ { for j = 1; j <= rows-i; j++ { fmt.Print(" ") } for k := 0; k != (2*i - 1); k++ { fmt.Print("$") } fmt.Println() } }

輸出

Golang program to create pyramid and pattern
      $
     $$$
    $$$$$
   $$$$$$$
  $$$$$$$$$
 $$$$$$$$$$$
$$$$$$$$$$$$$

程式碼描述

  • 在上面的程式中,我們首先宣告 main 包。

  • 我們匯入了 fmt 包,其中包含 fmt 包的檔案。

  • 現在啟動函式 main (),此函式是可執行程式的入口點。它不接受任何引數也不返回任何內容。

  • 宣告三個整型變數 i、j 和 rows。將 rows 變數初始化為所需的 $ 形圖案金字塔的整數值。使用 fmt.Scanln () 讀取並存儲 rows 值。

  • 使用 for 迴圈:條件在 if 語句中給出,並在條件正確時停止執行。

  • 最後,使用 fmt.Println () 函式在螢幕上打印出 $ 形圖案的金字塔結果。

示例 2:建立數字圖案金字塔的 Go 語言程式碼

演算法

  • 步驟 1 − 匯入 fmt 包和 strconv 包。

  • 步驟 2 − 啟動函式 main ()

  • 步驟 3 − 宣告並初始化變數 rows。

  • 步驟 4 − 初始化 k=0,NUM1=0,rows=5,NUM2=0。

  • 步驟 5 − 使用帶有條件和增量的 for 迴圈。

  • 步驟 5 − 使用 fmt.Println () 列印結果。

示例

package main // fmt package provides the function to print anything import "fmt" func main() { // DECLARE AND INITIALIZE THE INTEGER VARIABLES var rows, NUM1, NUM2, k int = 5, 0, 0, 0 // USING FOR LOOP for i := 1; i <= rows; i++ { k = 0 for space := 1; space <= rows-i; space++ { fmt.Print(" ") NUM1++ } for { if k == 2*i-1 { break } if NUM1 <= rows-1 { fmt.Printf("%d ", i+k) NUM1++ } else { NUM2++ fmt.Printf("%d ", (i + k - 2*NUM2)) } k++ } NUM2, k, NUM1 = 0, 0, 0 fmt.Println("") } // PRINT THE PYRAMID WITH NUMBER PATTERN }

輸出

    1
   2 3 2
  3 4 5 4 3
 4 5 6 7 6 5 4
5 6 7 8 9 8 7 6 5

程式碼描述

  • 在上面的程式中,我們首先宣告 main 包。

  • 我們匯入了 fmt 包,其中包含 fmt 包的檔案。

  • 現在啟動函式 main (),此函式是可執行程式的入口點。它不接受任何引數也不返回任何內容。

  • 宣告四個整型變數 NUM1、NUM2、k 和 rows。將 rows 變數初始化為所需的數字圖案金字塔的整數值。使用 fmt.Scanln () 讀取並存儲 rows 值。

  • 使用 for 迴圈  條件在 if 語句中給出,並在條件正確時停止執行。

  • 最後,使用 fmt.Println () 函式在螢幕上打印出數字圖案的金字塔結果。

示例 3:建立字母圖案金字塔的 Go 語言程式程式碼

演算法

  • 步驟 1 − 匯入 fmt 包和 strconv 包。

  • 步驟 2 − 啟動函式 main ()

  • 步驟 3 − 宣告並初始化變數。

  • 步驟 4 − 使用帶有條件和增量的 for 迴圈。

  • 步驟 5 − 使用 fmt.Println() 列印結果。

示例

package main // fmt package provides the function to print anything import "fmt" func main() { // declare and initialize the integer variable var rows int rows = 7 fmt.Scanf("%d", &rows) fmt.Printf("Golang program to create pyramid and pattern\n") alphabet := 64 i := 1 // using for loop for i <= rows { j := 1 for j <= rows-i { fmt.Printf(" ") j++ } k := i for 0 < k { fmt.Printf("%c", alphabet+k) k-- } l := 2 for l <= i { fmt.Printf("%c", alphabet+l) l++ } fmt.Printf("\n") i++ } // print the result }

輸出

Golang program to create pyramid and pattern
      A
     BAB
    CBABC
   DCBABCD
  EDCBABCDE
 FEDCBABCDEF
GFEDCBABCDEFG

程式碼描述

  • 在上面的程式中,我們首先宣告 main 包。

  • 我們匯入了 fmt 包,其中包含 fmt 包的檔案。

  • 現在啟動函式 main (),此函式是可執行程式的入口點。它不接受任何引數也不返回任何內容。

  • 宣告變數 rows 並將其初始化為所需的字母圖案金字塔的整數值。使用 fmt.Scanln () 讀取並存儲 rows 值。

  • 使用 for 迴圈:條件在 if 語句中給出,並在條件正確時停止執行。

  • 最後,使用 fmt.Println () 函式在螢幕上打印出字母圖案的金字塔結果。

結論

在以上三個示例中,我們已成功編譯並執行了 Go 語言程式程式碼,使用 for 迴圈建立了 $、數字和字母圖案的金字塔。

更新於: 2022年11月16日

1K+ 瀏覽量

啟動你的 職業生涯

透過完成課程獲得認證

開始學習
廣告

© . All rights reserved.