-->

9x9乘法表怎么编程

时间:2025-01-28 21:07:27 网络游戏

Python

```python

for i in range(1, 10):

for j in range(1, i + 1):

print(f"{i} * {j} = {i * j}", end="\t")

print()

```

C语言

```c

include

int main() {

int i, j;

for (i = 1; i <= 9; i++) {

for (j = 1; j <= i; j++) {

printf("%d * %d = %d\t", j, i, i * j);

}

printf("\n");

}

return 0;

}

```

Java

```java

public class Main {

public static void main(String[] args) {

for (int i = 1; i <= 9; i++) {

for (int j = 1; j <= i; j++) {

System.out.printf("%d * %d = %d\t", j, i, i * j);

}

System.out.println();

}

}

}

```

JavaScript

```javascript

for (let i = 1; i <= 9; i++) {

let row = '';

for (let j = 1; j <= i; j++) {

row += `${j} * ${i} = ${i * j}\t`;

}

console.log(row);

}

```

C++

```cpp

include

int main() {

for (int i = 1; i <= 9; i++) {

for (int j = 1; j <= i; j++) {

std::cout<< j << " * "<< i << " = " << i * j << "\t";

}

std::cout << std::endl;

}

return 0;

}

```

Ruby

```ruby

(1..9).each do |i|

(1..i).each do |j|

print "{j} * {i} = {i * j}\t"

end

puts

end

```

这些代码示例分别使用Python、C语言、Java、JavaScript、C++和Ruby编程语言实现了9x9乘法表。每段代码都采用了嵌套循环结构,并通过格式化输出来确保乘法表的对齐和美观。你可以选择任意一种语言进行尝试,根据你的编程习惯和需求选择最适合的方法。