日期:2014-05-20  浏览次数:20644 次

java循环编程
编程模块:
打印如下的图形:
5 4 4 4 5 
3 5 4 5 2 
3 3 5 2 2 
3 5 1 5 2 
5 1 1 1 5 


------解决方案--------------------
我好像一点规律都没有看出来!只能这样了:

Java code
System.out.println("5 4 4 4 5");
System.out.println("3 5 4 5 2");
System.out.println("3 3 5 2 2");
System.out.println("3 5 1 5 2");
System.out.println("5 1 1 1 5");

------解决方案--------------------
哦,好像看出规律来了,从矩形对角线下三角开始逆时针在四个三角中填充 1、2、3、4,对角线使用 5 填充,呵呵。
------解决方案--------------------
探讨
哦,好像看出规律来了,从矩形对角线下三角开始逆时针在四个三角中填充 1、2、3、4,对角线使用 5 填充,呵呵。

------解决方案--------------------
目前只能想出这个笨办法,呵呵,明天再想想看

Java code
public class ConsoleGraph {

    public static void main(String[] args) {
        printGraph(5);
    }

    private static void printGraph(int line) {
        int s = line / 2;
        for(int i = -s; i <= s; i++) {          
            for(int j = -s; j <= s; j++) {                
                if(j > -s) {
                    System.out.print(' ');
                }      
                if(Math.abs(i) == Math.abs(j)) {
                    System.out.print(5);
                    continue;
                }                
                if(i > 0 && Math.abs(j) < s) {
                    System.out.print(1);
                    continue;
                }
                if(j > 0 && Math.abs(i) < s) {
                    System.out.print(2);
                    continue;
                }
                if(i < 0 && Math.abs(j) < s) {
                    System.out.print(3);
                    continue;
                }
                if(j < 0 && Math.abs(i) < s) {
                    System.out.print(4);
                    continue;
                }
            }
            System.out.println();
        }
    }
}

------解决方案--------------------
有规律!