java九九乘法表,内涵2种方法(基础)

1

双for打印99乘法表,和for+if打印99乘法表。

public class Multiplication_table {
    
    
    public static void main(String[] args) {
    
    
        System.out.println("我是用双for写的:");
        for (int j = 1; j < 10; j++) {
    
    
            for (int i = 1; i <= j; i++) {
    
    
                System.out.print(i + "*" + j + "=" + i * j + "\t");
            }
            System.out.println();
        }
        Multiplication_2 m2 = new Multiplication_2();
        m2.test_if();
    }
}
//第二种方法
class Multiplication_2 {
    
    
    public void test_if() {
    
    
        System.out.println("我是用for+if写的:");
        for (int i = 1, j = 1; i < 10; ) {
    
    
            if (i > j) {
    
    
                System.out.print(i + "*" + j + "=" + i * j + "\t");
            } else if (i < j) {
    
    
                if (i == 1) {
    
    
                    System.out.println();
                }
                System.out.print(i + "*" + j + "=" + i * j + "\t");
                i++;
            } else {
    
    
                System.out.print(i + "*" + j + "=" + i * j + "\t");
                i = 1;
                j++;
                if (j > 9) {
    
    
                    break;
                }
            }
        }
    }
}

感谢观看,求一个赞❤

猜你喜欢

转载自blog.csdn.net/weixin_45380885/article/details/109127733