编写一个 Go 程序来打印 1 的方形数字图案。在此 Golang 方形数字图案示例中,嵌套的 for 循环迭代方形的边。在循环内,我们以方形形状打印 1。
package main
import "fmt"
func main() {
var i, j, sqSide int
fmt.Print("Enter Any Side of a Square to Print 1's = ")
fmt.Scanln(&sqSide)
fmt.Println("Square Pattern of 1's")
for i = 0; i < sqSide; i++ {
for j = 0; j < sqSide; j++ {
fmt.Print("1 ")
}
fmt.Println()
}
}
Enter Any Side of a Square to Print 1's = 8
Square Pattern of 1's
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 1 1
1 1 1 1 1 1 1 1
Golang 程序打印 0 的方形数字图案
在此方形数字图案示例中,我们将 0 替换为 1。
package main
import "fmt"
func main() {
var i, j, sqSide int
fmt.Print("Enter Any Side of a Square to Print 0's = ")
fmt.Scanln(&sqSide)
fmt.Println("Square Pattern of 0's")
for i = 0; i < sqSide; i++ {
for j = 0; j < sqSide; j++ {
fmt.Print("0 ")
}
fmt.Println()
}
}
Enter Any Side of a Square to Print 0's = 9
Square 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 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
此 Go 程序允许输入任何数字,并以方形图案打印该数字。
package main
import "fmt"
func main() {
var i, j, side, num int
fmt.Print("Enter Any Side of a Square = ")
fmt.Scanln(&side)
fmt.Print("Number to Print as the Square Pattern = ")
fmt.Scanln(&num)
fmt.Println("Numeric Square Pattern")
for i = 0; i < side; i++ {
for j = 0; j < side; j++ {
fmt.Printf("%d ", num)
}
fmt.Println()
}
}
