码迷,mamicode.com
首页 > 编程语言 > 详细

一个算法题目: 三角形

时间:2018-06-10 15:02:33      阅读:192      评论:0      收藏:0      [点我收藏+]

标签:param   static   数字   ++   结果   new   int   []   arp   

 

输出结果

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

 

一个算法题目: 三角形

标签:param   static   数字   ++   结果   new   int   []   arp   

原文地址:https://www.cnblogs.com/yszzu/p/9162606.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!