怎么编写Java程序打印九九乘法表?

如题所述

第1个回答  2020-08-06
public static void main(String[] args) {
for (int i = 1; i <= 9; i++) {
for (int j = 1; j <= i; j++) {
System.out.print(j + "*" + i + "=" + (i * j) + "\t");
}
System.out.println();
}
}
第2个回答  2019-04-07

需求:在控制台输出九九乘法表。

首先我们写出九九乘法表:

1*1=1

1*2=2 2*2=4

1*3=3 2*3=6 3*3=9

1*4=4 2*4=8 3*4=12 4*4=16

...

1*9=9 2*9=18 3*9=27 ...

我们先把这个九九乘法表看出是这样的一个形状:

*

**

***

****

*****

******

*******

********

*********

先尝试打印出*,再尝试打印九九乘法表就会简单很多。

代码如下:

class Demo {
public static void main(String[] args) {
        //此循环打印出*
for(int x=0; x<9; x++) {
for(int y=0; y<=x; y++) {
System.out.print("*");
}
System.out.println();
}
System.out.println("--------------");

//为了更加方便使用数据,我们从1开始
for(int x=1; x<=9; x++) {
for(int y=1; y<=x; y++) {
System.out.print(y+"*"+x+"="+y*x+"\t");
}
System.out.println();
}
}
}

相关了解……

你可能感兴趣的内容

本站内容来自于网友发表,不代表本站立场,仅表示其个人看法,不对其真实性、正确性、有效性作任何的担保
相关事宜请发邮件给我们
© 非常风气网