编写一个 Go 程序来打印 1 的空心框数字图案。在这个 Golang 空心框数字图案示例中,嵌套的 for 循环迭代行和列。if 语句(if i == 1 || i == row || j == 1 || j == col)检查是否是第一行或第一列或最后一行或最后一列。如果为真,则打印 1;否则,打印空格。
package main
import "fmt"
func main() {
var i, j, row, col int
fmt.Print("Enter the Hollow Box Rows and Columns = ")
fmt.Scan(&row, &col)
fmt.Println("Hollow Box Number Pattern")
for i = 1; i <= row; i++ {
for j = 1; j <= col; j++ {
if i == 1 || i == row || j == 1 || j == col {
fmt.Print("1 ")
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
}
Enter the Hollow Box Rows and Columns = 7 22
Hollow Box Number Pattern
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
1 1
1 1
1 1
1 1
1 1
1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1 1
Golang 程序打印 0 的空心框数字图案
在这个空心框数字图案示例中,我们将 0 替换为 1。
package main
import "fmt"
func main() {
var i, j, row, col int
fmt.Print("Enter the Hollow Box of 0's Rows and Columns = ")
fmt.Scan(&row, &col)
fmt.Println("Hollow Box Number Pattern of 0's")
for i = 1; i <= row; i++ {
for j = 1; j <= col; j++ {
if i == 1 || i == row || j == 1 || j == col {
fmt.Print("0 ")
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
}
Enter the Hollow Box of 0's Rows and Columns = 8 20
Hollow Box Number Pattern of 0's
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
0 0
0 0
0 0
0 0
0 0
0 0
0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0 0
此 Golang 示例允许输入数字,并以空心框图案打印该数字。
package main
import "fmt"
func main() {
var i, j, row, col, num int
fmt.Print("Enter the Hollow Box Rows and Columns = ")
fmt.Scan(&row, &col)
fmt.Print("Enter the Number to Print Hollow Box = ")
fmt.Scanln(&num)
fmt.Println("Hollow Box Number Pattern")
for i = 1; i <= row; i++ {
for j = 1; j <= col; j++ {
if i == 1 || i == row || j == 1 || j == col {
fmt.Printf("%d ", num)
} else {
fmt.Print(" ")
}
}
fmt.Println()
}
}
