一个算法题目: 三角形

 

输出结果

5 4 3 2 1 a
4 3 2 1 b
3 2 1 c
2 1 d
1 e

 

public class Test {

    /**
     * @param args
     */
    public static void main(String[] args) {

        NumPrinter numPrinter = new NumPrinter();
        CharPrinter charPrinter = new CharPrinter();
        int charst = 97;
        for (int st = 5; st >= 1; st--) {
            numPrinter.print(st);
            charPrinter.print(charst++);
            System.out.println();
        }
    }

}

class NumPrinter {

    public void print(int i) {
        for (; i >= 1; i--) {
            System.out.print(i + " ");
        }
    }

}

class CharPrinter {
    public void print(int i) {
        System.out.print((char) i);
    }
}

 

关键是字符的输出,因为前面的数字是递减的,而字符是递增的,因此需要使用数字强转成char

 

posted @ 2018-06-10 11:37  funny_coding  阅读(128)  评论(0编辑  收藏  举报
build beautiful things, share happiness