【Java学习】空心金字塔+空心菱形

              *            
             * *        
            *   *        
           *     *        
         *********        
         


import java.util.Scanner;

public class Pyramid{
	public static void main(String[] args){

		Scanner sc = new Scanner(System.in);
		System.out.println("please enter totalLevel of Pyramid");
		int totalLevel = sc.nextInt();
		for(int i = 1 ; i <= totalLevel ; i++){
			for(int k = 1 ; k <= totalLevel - i ; k++){
				System.out.print(" ");
			}
			for(int j = 1 ; j <= 2 * i - 1 ; j++){
				if(j == 1 || j == 2 * i - 1 || i == totalLevel){
					System.out.print("*");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println("");
		}


	}
}
//空心菱形
import java.util.Scanner;

public class HollowDiamond{
	public static void main(String[] args){
		Scanner sc = new Scanner(System.in);
		System.out.println("please enter the Number of the sides of the diamond:");
		int sideNums = sc.nextInt();
		for(int i = 1 ; i <= sideNums ; i++){
			for(int k = 1 ; k <= sideNums - i ; k++){
				System.out.print(" ");
			}
			for(int j = 1 ; j <= 2 * i - 1 ; j++){
				if(j == 1 || j == 2 * i - 1){
					System.out.print("*");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println("");
		}

		for(int i = sideNums ; i >= 1 ; i--){
			for(int k = sideNums - i ; k >= 1 ; k--){
				System.out.print(" ");
			}
			for(int j = 2 * i - 1 ; j >= 1 ; j--){
				if(j == 1 || j == 2 * i - 1){
					System.out.print("*");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println("");
		}
	}
}

 

你可能感兴趣的:(java,学习,算法)