打印图形

版权声明:本文为博主原创文章,未经博主允许不得转载。 https://blog.csdn.net/wo3002807/article/details/12713905

这是各种打印各种图形。

/**
 * 作者:陈德金
 * 时间:2013.09.23
 * 功能:打印出各种图形
 * @author Administrator
 *
 */
public class Print {

	public static void main(String[] args) {
		
		
		// TODO Auto-generated method stub
//		Print1(7);
//		Print2(7);
		diamonds(5);
	}
	/*效果如下:
	*
	**
	***
	****
	*****
	******
	*******
	******
	*****
	****
	***
	**
	*
	*/	
	public static void Print1(int n){
		for(int i=1; i<=n ;i++){
			for(int j=0; j < i; j++){
				System.out.print("*");
			}
			System.out.println();			
		}
		for(int i=n-1; i >= 1; i--){
			for(int j=0; j < i;j++){
				System.out.print("*");
			}
			System.out.println();
		}
	}
	/*
	 * 效果如下:
	  *
          * *
         * * *
        * * * *
       * * * * *
      * * * * * *
     * * * * * * *
     * 
     * 原理:?????
     */
	public static void Print2(int n){
		//1,逐步输出每一行的数据
		for(int i=1; i<=n; i++){
			//2,输出前面空字符串
			for(int j=n-i; j > 0; j--){
				System.out.print(" ");
			}
			//3,
			for(int k = 0; k < 2*i-1 ; k++){
				//4,判断
				if(k%2!=0)
					System.out.print(" ");
				else
					System.out.print("*");
			}
			System.out.println();
		}
	}
	/*
	 * 
	 * 这是我参加群硕笔试的题目:
	   *
           ***
          *****
           ***
            *
	 */
	public static void diamonds(int n){
		if(n%2==0){
			System.out.println("该数值不是单数!");
			return ;
		}
		for(int i=1 ; i <= n; i+=2){
			for(int j=n/2;j > i/2; j--){
				System.out.print(" ");
			}
			for(int k = 1; k<=i;k++){
				System.out.print("*");
			}
			System.out.println();
		}
		for(int i=n-2 ; i >=1; i-=2){
			for(int j=n-i;j >= 1; j-=2){
				System.out.print(" ");
			}
			for(int k = 1; k<=i;k++){
				System.out.print("*");
			}
			System.out.println();
		}
	}
}



 

猜你喜欢

转载自blog.csdn.net/wo3002807/article/details/12713905