这个 Go 程序使用 If Else 语句来查找两个数字中的最大值。if 条件检查 num1 是否大于 num2,如果为真,则 num1 是最大的。否则,它会打印 else 语句。
package main
import "fmt"
func main() {
var num1, num2 int
fmt.Print("Enter the First Number to find Largest of Two = ")
fmt.Scanln(&num1)
fmt.Print("Enter the Second Number to find Largest of Two = ")
fmt.Scanln(&num2)
if num1 > num2 {
fmt.Println("The Largest amoung two = ", num1 )
} else {
fmt.Println("The Largest amoung two = ", num2 )
}
}

Go 程序查找两个数字中的最大值。
此 Golang 程序使用 Else If 语句来计算两个数字中的最大值。
- if num1 > num2 – 检查 num1 是否大于 num2。如果为真,则 num1 最大。
- else if num2 > num1 – 检查 num2 是否大于 num1。如果为真,则 num2 最大。
- 如果以上两个条件都失败,则它们相等。
package main
import "fmt"
func main() {
var num1, num2 int
fmt.Print("Enter the First Number to find Largest of Two = ")
fmt.Scanln(&num1)
fmt.Print("Enter the Second Number to find Largest of Two = ")
fmt.Scanln(&num2)
if num1 > num2 {
fmt.Println("The Largest amoung two = ", num1 )
} else if num2 > num1 {
fmt.Println("The Largest amoung two = ", num2 )
} else {
fmt.Println("Both of them are Equal")
}
}
SureshMac:Goexamples suresh$ go run LargestofTwo1.go
Enter the First Number to find Largest of Two = 10
Enter the Second Number to find Largest of Two = 5
The Largest amoung two = 10
SureshMac:Goexamples suresh$ go run LargestofTwo1.go
Enter the First Number to find Largest of Two = 9
Enter the Second Number to find Largest of Two = 18
The Largest amoung two = 18
SureshMac:Goexamples suresh$ go run LargestofTwo1.go
Enter the First Number to find Largest of Two = 9
Enter the Second Number to find Largest of Two = 9
Both of them are Equal
在此 Golang 程序中,我们使用 switch case 来打印两个数字中较大的一个。
package main
import "fmt"
func main() {
var num1, num2 int
fmt.Print("Enter the First Number to find Largest of Two = ")
fmt.Scanln(&num1)
fmt.Print("Enter the Second Number to find Largest of Two = ")
fmt.Scanln(&num2)
switch {
case num1 > num2:
fmt.Println("The Largest amoung two = ", num1)
case num1 < num2:
fmt.Println("The Largest amoung two = ", num2)
default:
fmt.Println("Both are Equal")
}
}
SureshMac:Goexamples suresh$ go run LargestofTwo2.go
Enter the First Number to find Largest of Two = 99
Enter the Second Number to find Largest of Two = 88
The Largest amoung two = 99
SureshMac:Goexamples suresh$ go run LargestofTwo2.go
Enter the First Number to find Largest of Two = 77
Enter the Second Number to find Largest of Two = 90
The Largest amoung two = 90
SureshMac:Goexamples suresh$ go run LargestofTwo2.go
Enter the First Number to find Largest of Two = 1
Enter the Second Number to find Largest of Two = 1
Both are Equal