编写一个 Go 语言程序来打印 1 和 0 的方框数字图案。在此 Golang 方框图案示例中,嵌套的 for 循环迭代行和列。if 语句(if i == 1 || i == row || j == 1 || j == col)检查是否为第一行、第一列、最后一行或最后一列。如果为真,则打印 1;否则,打印 0。
package main
import "fmt"
func main() {
var i, j, row, col int
fmt.Print("Enter the Box Number Pattern Rows = ")
fmt.Scanln(&row)
fmt.Print("Enter the Box Number Pattern Columns = ")
fmt.Scanln(&col)
fmt.Println("Box Number Pattern of 1's and 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("1 ")
} else {
fmt.Print("0 ")
}
}
fmt.Println()
}
}

Golang 打印 0 和 1 方框数字图案的程序
在此 1 和 0 的方框数字图案示例中,我们交换了 0 和 1。
package main
import "fmt"
func main() {
var i, j, row, col int
fmt.Print("Enter the Box Number Pattern Rows = ")
fmt.Scanln(&row)
fmt.Print("Enter the Box Number Pattern Columns = ")
fmt.Scanln(&col)
fmt.Println("Box Number Pattern of 1's and 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("1 ")
}
}
fmt.Println()
}
}
Enter the Box Number Pattern Rows = 7
Enter the Box Number Pattern Columns = 12
Box Number Pattern of 1's and 0's
0 0 0 0 0 0 0 0 0 0 0 0
0 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 0
0 1 1 1 1 1 1 1 1 1 1 0
0 0 0 0 0 0 0 0 0 0 0 0
此 Golang 程序允许输入方框的外部和内部值。接下来,它打印 x 作为方框的外部数字,y 作为方框的内部数字。
package main
import "fmt"
func main() {
var i, j, row, col, x, y int
fmt.Print("Enter the Box Number Pattern Rows and Columns = ")
fmt.Scan(&row, &col)
fmt.Print("Enter the Outside and Inside Values = ")
fmt.Scanln(&x, &y)
fmt.Println("Box Number Pattern of 1's and 0's")
for i = 1; i <= row; i++ {
for j = 1; j <= col; j++ {
if i == 1 || i == row || j == 1 || j == col {
fmt.Printf("%d ", x)
} else {
fmt.Printf("%d ", y)
}
}
fmt.Println()
}
}
Enter the Box Number Pattern Rows and Columns = 6 20
Enter the Outside and Inside Values = 9 4
Box Number Pattern of 1's and 0's
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9
9 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9
9 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9
9 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9
9 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 4 9
9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9 9