Golang 打印左三角星形

Golang 打印左三角星形

在本教程中,我们将编写一段 Go 语言代码来打印左三角星形图案。我们将展示如何在 Go 语言中打印星形图案。

*
  * *
 * * *
* * * *

如何打印星形图案

如上所示,左三角星形图案中,随着行数的增加,星星数量也在增加。该图案的规律是第一行有1个星星,第二行有2个星星,以此类推。

我们将使用两个for循环来实现,第一个for循环用于打印空格,第二个for循环用于在相应位置打印星号字符。

For loop:
for [condition | ( init; condition; increment) | Range] { 
   statement(s); 
}

示例1:使用For循环打印左倾斜星形图案的Golang程序

步骤

  • 步骤1 - 导入fmt包。

  • 步骤2 - 开始函数 main()

  • 步骤3 - 声明并初始化整型变量(row=要打印的行数)。

  • 步骤4 - 使用第一个内部for循环来迭代从1到“行数”的行。

  • 步骤5 - 使用第二个内部for循环来迭代从1到i的列,以打印星形图案。

  • 步骤6 - 外部for循环用于计数行号。

  • 步骤7 - 打印完一行的所有列后,换行即打印新行。

示例

//GOLANG PROGRAM TO PRINT LEFT ANGLED STAR PATTERN
package main

// fmt package provides the function to print anything
import "fmt"

// this is the main function
func main() {

   //declaring variables with integer datatype
   var i, j, k, row int

   // assigning the row variable with the value of number of rows
   row = 5

   // Print the pattern
   fmt.Println("\nLeft Angled Star Pattern")

   // displaying the pattern using for loops
   for i = 0; i <= row; i++ {

      //printing the spaces
      for j = 0; j < row-i; j++ {
         fmt.Print(" ")
      }
      //printing the stars
      for k = 0; k < i; k++ {
         fmt.Print("*")
      }
      // printing new line
      fmt.Println()
   }
}

输出

Left Angled Star Pattern 

    *   
   ** 
  *** 
 **** 
*****

代码描述

  • 在上面的代码中,首先我们导入了主要的包和fmt包,fmt包允许我们在屏幕上打印任何内容。

  • 然后开始main()函数。

  • 声明并初始化int类型的变量i、j、k和row。

  • 让我们给行数赋值为5,即为了这个示例,让我们打印一个有5行的星型图案。

  • i、j和k用于迭代for循环,它们将选择相应的行和列以打印*字符。

  • 为了打印这种类型的图案,我们需要在另一个for循环内使用一个for循环。第一个for循环用于选择行,它从1迭代到row变量包含的行数。

  • 第二个for循环从j = 0开始,到当前行-1的值。

  • 第三个for循环用于打印*字符。

  • 使用fmt.Print()函数在第三个for循环内打印*字符。

  • 然后打印一个换行。

  • 以这种方式打印左侧三角形图案。

示例2:使用递归打印左角度星型图案的Go语言程序

步骤

  • 步骤1 - 导入fmt包。

  • 步骤2 - 定义printSpace()函数。

  • 步骤3 - 定义Star()函数。

  • 步骤4 - 定义pattern()函数。

  • 步骤5 - 开始main()函数。

  • 步骤6 - 声明并初始化整数变量。

  • 步骤7 - 调用pattern()函数。

示例

// GOLANG PROGRAM TO PRINT LEFT ANGLED STAR PATTERN USING RECURSION
package main

// fmt package provides the function to print anything
import "fmt"

// defining printSpace() function
func printSpace(number int) {

   // defining base condition
   if number == 0 {
      return
   }

   // printing the spaces
   fmt.Printf(" ")

   // calling the printSpace() function recusrsively
   printSpace(number - 1)
}
func star(number int) {
   // defining base condition
   if number == 0 {
      return
   }

   // printing the * character
   fmt.Printf("*")

   // calling the star() function recursively
   star(number - 1)
}
func pattern(number, n int) {
   // defining base case
   if number == 0 {
      return
   }

   // calling the printSpace() function to print spaces
   printSpace(number - 1)

   // calling the star function to print * patterns
   star((n - number) + 1)

   // printing new line
   fmt.Println()

   // calling the pattern() function recursively
   pattern(number-1, n)
}
// calling the main function
func main() {

   // declaring variable row
   var row int = 7

   // calling the star() function
   pattern(row, row)
}

输出

* 
     ** 
    *** 
   **** 
  ***** 
 ****** 
*******

代码说明

  • 在上述代码中,首先我们导入了主要的包和fmt包,fmt包允许我们将任何东西打印到屏幕上。

  • 创建了三个函数,分别命名为printSpace(),star()和pattern()。

  • printSpace()函数将以递归的方式打印所选行数的空格。

  • star()函数将以递归的方式在相应的行中打印*字符。

  • 第三个函数pattern()将逐个调用每个函数,并打印新行。

  • 然后开始main()函数。

  • 初始化row变量,数据类型为int,并将要打印的星型图案的行数存储在其中。

  • 调用pattern()函数,并将row变量作为参数传递给它。

结论

我们已经成功编译和执行了一个Go语言程序,用于打印一个左倾斜的星型图案。

为了打印这个星型图案,我们使用了递归,在其中递归调用了三个函数。

Camera课程

Python教程

Java教程

Web教程

数据库教程

图形图像教程

办公软件教程

Linux教程

计算机教程

大数据教程

开发工具教程