编写一个 Go 语言程序来打印 1 的矩形数字模式。在此 Golang 矩形数字模式示例中,嵌套的 for 循环迭代矩形的行和列,并输出 1。
package main
import "fmt"
func main() {
var i, j, recRow, recCol int
fmt.Print("Enter the Rows to Print Rectangle of 1's = ")
fmt.Scanln(&recRow)
fmt.Print("Enter the Columns to Print Rectangle of 1's = ")
fmt.Scanln(&recCol)
fmt.Println("Rectangle Pattern of 1's")
for i = 0; i < recRow; i++ {
for j = 0; j < recCol; j++ {
fmt.Print("1 ")
}
fmt.Println()
}
}

Golang 打印 0 的矩形数字模式的程序
在此矩形数字模式示例中,我们将 0 替换为 1。
package main
import "fmt"
func main() {
var i, j, recRow, recCol int
fmt.Print("Enter the Rows to Print Rectangle of 0's = ")
fmt.Scanln(&recRow)
fmt.Print("Enter the Columns to Print Rectangle of 0's = ")
fmt.Scanln(&recCol)
fmt.Println("Rectangle Pattern of 0's")
for i = 0; i < recRow; i++ {
for j = 0; j < recCol; j++ {
fmt.Print("0 ")
}
fmt.Println()
}
}
Enter the Rows to Print Rectangle of 0's = 6
Enter the Columns to Print Rectangle of 0's = 15
Rectangle 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 0 0 0 0 0 0 0 0 0
此 Go 程序允许输入任何数字,并以矩形模式打印该数字。
package main
import "fmt"
func main() {
var i, j, recRow, recCol, num int
fmt.Print("Enter the Rows to Print Rectangle of 1's = ")
fmt.Scanln(&recRow)
fmt.Print("Enter the Columns to Print Rectangle of 1's = ")
fmt.Scanln(&recCol)
fmt.Print("Enter any Number to Print as Rectangle = ")
fmt.Scanln(&num)
fmt.Println("Rectangle Pattern of 1's")
for i = 0; i < recRow; i++ {
for j = 0; j < recCol; j++ {
fmt.Printf("%d ", num)
}
fmt.Println()
}
}
Enter the Rows to Print Rectangle of 1's = 8
Enter the Columns to Print Rectangle of 1's = 22
Enter any Number to Print as Rectangle = 8
Rectangle Pattern of 1's
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8
8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8 8