方式一
package mainimport "fmt"func main() {// Iterate over the rows (1 to 9)for row := 1; row <= 9; row++ {// Iterate over the columns (1 to row)for col := 1; col <= row; col++ {// Calculate the productproduct := row * col// Print the productfmt.Printf("%d x %d = %d\t", row, col, product)}// Print a newline after each rowfmt.Println()}
}
方式二
package mainimport "fmt"func printTable(row, col int) {if row > 9 || col > row {return // Base case: exit when row or col exceeds limits}product := row * colfmt.Printf("%d x %d = %d\t", row, col, product)// Print next column in the same rowprintTable(row, col+1)// If at the end of the row, move to the next row with col 1if col == row {fmt.Println()printTable(row+1, 1)}
}func main() {printTable(1, 1)
}