使用库函数获取整数前驱的 Go 语言程序


本文将讨论如何在 Go 语言中使用库函数获取整数的前驱。

整数的前驱定义为其前面的数。例如,2 的前驱是 2 – 1 = 1。类似地,-50 的前驱是 -51,依此类推。

语法

Syntax for Println() = fmt.Println(...interface{})

示例 1:我们将使用库函数获取整数的前驱

算法

  • 步骤 1 − 导入 fmt 包。

  • 步骤 2 − 启动 main 函数。

  • 步骤 3 − 初始化一个 int 类型变量并存储值。

  • 步骤 4 − 实现查找前驱的逻辑。

  • 步骤 5 − 在屏幕上打印结果。

示例

Open Compiler
// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY // fmt package allows us to print anything on the screen. package main import "fmt" // fmt package allows us to print anything on the screen. // calling the main() function func main() { fmt.Println("Golang program to find the predecessor of an integer number using library function") // initializing the number variable to store the required number. var number, predecessor int // assigning value to the number variable. number = 49 // implementing the logic predecessor = number - 1 // Print the result fmt.Println(" predecessor of", number, "is ", predecessor ) }

输出

Golang program to find the predecessor of an integer number using library function
predecessor of 49 is 48

代码描述

  • 首先,我们导入 fmt 包,该包允许我们打印任何内容。

  • 然后我们启动main()函数。

  • 之后,我们需要将数字赋给一个变量并创建一个前驱变量。

  • 然后创建查找前一个整数的逻辑。

  • 使用fmt.Println()函数在屏幕上打印结果。

示例 2:我们将使用库函数获取整数的前驱,不带任何参数且不返回值

算法

  • 步骤 1 − 导入 fmt 包和包。

  • 步骤 2 − 启动main()函数。

  • 步骤 3 − 调用predecessor()函数。

  • 步骤 4 − 启动predecessor()函数。

  • 步骤 5 − 声明并初始化变量。

  • 步骤 6 − 使用fmt.Printf()在控制台屏幕上打印结果。

示例

Open Compiler
// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY FUNCTION // fmt package allows us to print anything on the screen. package main import "fmt" // function prototype // GO program execution starts with the function main() func main() { // function call predecessor() } func predecessor() { fmt.Println("Golang program to find the predecessor of an integer number using library function") // declare and initialize the variable var c int c = 4 // print the result fmt.Println("predeccessor of the number",c,"=",c - 1) }

输出

Golang program to find the predecessor of an integer number using library function
predeccessor of the number 4 = 3

代码描述

  • 首先,我们导入了 fmt 包,该包允许我们打印任何内容。

  • 现在让我们启动function main()。GO 程序执行从 main() 函数开始。

  • 接下来我们调用predecessor()函数。

  • 现在我们启动predecessor()函数。声明并初始化整数变量 c,需要找到其前驱。

  • 在上面的程序中,predecessor()函数执行计算,并且没有参数传递给此函数。此函数的返回类型为空,因此没有返回值。

  • 最终结果使用内置函数 fmt.Printf() 在控制台屏幕上打印。此函数在 fmt 包中定义,它有助于写入标准输出。

Explore our latest online courses and learn new skills at your own pace. Enroll and become a certified expert to boost your career.

示例 3:我们将使用库函数在两个独立的函数中获取整数的前驱,带参数和返回值

算法

  • 步骤 1 − 导入 fmt 包

  • 步骤 2 − 创建并定义predecessor()函数。

  • 步骤 3 − 启动 main函数()

  • 步骤 4 − 从用户获取值。

  • 步骤 5 − 调用上面创建的predecessor()函数,并将用户输入的值传递给它。

  • 步骤 6 − 在屏幕上打印结果。

示例

Open Compiler
// GOLANG PROGRAM TO FIND THE PREDECESSOR OF AN INTEGER NUMBER USING LIBRARY // fmt package allows us to print anything on the screen. package main import "fmt" func predecessor(number int) int { var n int n = number - 1 return n } // calling the main() function func main() { fmt.Println("Golang program to find the predecessor of an integer number using library function") // declare the variables var number, previous int // initialize the number variable number = 66 // implementing the logic. // function call previous = predecessor(number) // print the result fmt.Println(" predecessor of", number, "is ", previous ) }

输出

Golang program to find the predecessor of an integer number using library function
predecessor of 66 is 65

代码描述

  • 首先,我们导入了 fmt 包,该包允许我们打印任何内容。

  • 然后创建并定义前驱函数,该函数将返回用户输入的整数的前一个值。

  • 然后我们启动了main()函数。

  • 我们声明整数变量 number 和 previous。使用一个值初始化变量 number 以查找其前驱并将结果存储在变量 previous 中。

  • 调用前驱函数并存储答案。

  • 最后使用fmt.Println()函数在屏幕上打印结果。

结论

我们已成功编译并执行了 Go 语言程序,以使用库函数查找整数的前驱,并附带示例。

更新于: 2022-11-16

174 次查看

开启你的 职业生涯

通过完成课程获得认证

立即开始
广告