Java_whilefor-->打印九九乘法表

得到结果如图的九九乘法表

下面是解题思路:先思考打印单行乘法表,通过for循环来实现。以第3行为例

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

for(int i=1;i<=3;i++){
System.out.ptintln(i+"*3="+i*3+"\t");
}
接下来以一行为例考虑多行,第3行的for循环判断终止条件是i<=3,不难得出第n行的for循环终止条件是i<=n;

则在单行for循环外加上一个判断第n行的for循环,通过两个for嵌套循环来输出九九乘法表;

整体代码如下: TestWhileFor02.java

public class TestWhileFor02 {
//输出九九乘法表   嵌套循环
    public static void main(String[] args) {
        // TODO Auto-generated method stub
        for (int n = 1; n <= 9; n++) {
            for (int i = 1; i <= n; i++) {
                System.out.print(i + "*" + n + "=" + (i * n) + "\t");
            }
            System.out.println();
        }

    }
}
}


猜你喜欢

转载自blog.csdn.net/weixin_39104294/article/details/79145504