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

java按规则输出下面的数字,在线等
要输出
1
1 2 1
1 2 3 2 1 
1 2 3 4 3 2 1 

thanks

------解决方案--------------------
Java code

import java.util.Scanner;

public class Test12 {

    public static void main(String[] args) {
        System.out.println("请输入一个1~15之间的整数:");
        Scanner scanner = new Scanner(System.in);
        
        int n = scanner.nextInt();
        for(int i =1; i<=n; i++){
            for(int j = 0; j<2*i-1; j++){
                if(i-j>0){
                    System.out.print(j+1 + "  ");
                }else{
                    System.out.print(2*i-j-1 + "  ");
                }
                
            }
            System.out.println();
        }
        
    }
}

------解决方案--------------------
Java code

    for(int i=0;i<4;i++){
            for(int j=0;j<2*i+1;j++){
                if(i >= j){
                    System.out.print(j+1+" ");
                }else{
                    System.out.print(2*i-j+1+" ");
                }
            }
            System.out.println();
        }

------解决方案--------------------
如果要灵活点,就照1L那样就可以了