Java执行语句-循环语句(for循环嵌套-打印三角形、菱形实例)

for循环的嵌套

  1. 需求1:打印以下图形
/**
			****
		    ****
			****
*/
import java.util.Scanner;
public class Test4{
	public static void main(String[] args){
		for(int i=0;i<3;i++){
			for(int j = 0;j<4;j++){
				System.out.print("*");
			}
			System.out.println();
		}
	}
}

2.需求2:打印以下图形

/**
   			 *
			 **
			 ***
			 ****
			 *****

*/

import java.util.Scanner;
	public class Test5{
	public static void main(String[] args){
		Scanner scan = new Scanner(System.in);
			for(int i =0;i<5;i++){
				for(int j =0;j<=i;j++){
					System.out.print("*");
				}
				System.out.println();
			}
		}
	}

3.需求3:打印以下图形

/**
   				 *
			    **
			   ***
			  ****
			 *****

*/

import java.util.Scanner;
public class Test6{
	public static void main(String[] args){
		
		for(int i =0;i<5;i++){
			for(int k=4;k>i;k--){
				System.out.print(" ");
			}
			for(int j=0;j<=i;j++){
				System.out.print("*");
			}
			System.out.println();
		}
	}
	
}

4.需求4

打印以下图形

/**
			*****
		   	****
		    ***
		    **
		    *        

*/
import java.util.Scanner;
public class Test7{
	public static void main(String[] args){
		
		for(int i =0;i<5;i++){
			
			for(int j=5;j>i;j--){
				System.out.print("*");
			}
			System.out.println();
		}
	}
	
}

5.需求5:打印以下图形

/**
			*****
		   	 ****
		      ***
		       **
		        *        

*/
import java.util.Scanner;
	public class Test8{
		
		public static void main(String[] args){

			for(int i=0;i<5;i++){
				for(int k = 0;k<i;k++){
					System.out.print(" ");
				}
				for(int j=5;j>i;j--){
					System.out.print("*");
				}
				System.out.println();
			}
		}
	}

6.需求6:打印以下图形

/**
			   *
			  ***
			 *****
			*******
*/

import java.util.Scanner;
public class Test9{
	public static void main(String[] args){
		
		for(int i = 0;i<4;i++){
			for(int k = 0;k<3-i;k++){
				System.out.print(" ");
			}
			for(int j = 0;j<2*i+1;j++){
				System.out.print("*");
			}
				System.out.println();
		}
	}
}

7.需求7:打印以下图形

/**
  			*
		   * *
          *   *
	   	 *******
*/
import java.util.Scanner;
public class Test10{
	public static void main(String[] args){
		
		for(int i=0;i<4;i++){
			for(int k = 0;k<3-i;k++){
				System.out.print(" ");
			}	
			for(int j =0;j<2*i+1;j++){
				//第一行||最后一行||每一行第一列||每一行最后一列
				if(i==0||i==3||j==0||j==2*i){
				System.out.print("*");
				}else{
				System.out.print(" ");	
				}	
				
			}
	
			System.out.println();
		}
	}
}

8.需求8:打印以下图形

/**
				*******
                 *****
                  ***
                   *
*/

import java.util.Scanner;
public class Test11{
	public static void main(String[] args){
		
		for(int i =0;i<4;i++){
			for(int k =0;k<i;k++){
				System.out.print(" ");
			}
			for(int j = 0;j<7-2*i;j++){
				System.out.print("*");
			}
			
			System.out.println();
		}
	}
}
  1. 需求9:打印以下图形
/**
			*******
		 	 *	 *
			  * *
               *


*/

import java.util.Scanner;
public class Test12{
	public static void main(String[] args){
		
		for(int i = 0;i<4;i++){
			for(int k = 0;k<i;k++){
				System.out.print(" ");
			}
			for(int j = 0;j<7-2*i;j++){
				//第一行||最后一行||第一行第一列||最后一行最后一列
				if(i==0||i==3||j==0||j==7-2*i-1){
					System.out.print("*");
				}else{
					System.out.print(" ");
				}
			}
			System.out.println();	
		}
	}
}

10.需求10:打印以下图形

/**
        *
	  *  *
	 *    *
	*      *
	 *    *
	  *  *
	    *
*/

//方法一:使用for循环打印
public class MyTest02{
		for(int i = 0;i<4;i++){
				for(int k = 0;k<3-i;k++){
					System.out.print(" ");
				}
				for(int j = 0;j<2*i+1;j++){
					if(j==0||j==2*i){
						System.out.print("*");
					}else{
						System.out.print(" ");
					}				
				}
				System.out.println();
			}
			for(int i = 0;i<3;i++){
				for(int k = 0;k<=i;k++){
					System.out.print(" ");
				}
				for(int j = 0;j<5-2*i;j++){
					if(j==0||j==4-2*i){
						System.out.print("*");
					 }else{
						 System.out.print(" ");
				}
			}
						System.out.println();
		}
}	
	
	
//方法二:使用for循环打印
public class MyTest02{
	//使用while
		public static void main(String[] args){
			
		int i = 0;
		
		
		while(i<4){
			int k = 0;
			while(k<3-i){
				System.out.print(" ");
				k++;
			}
			int j = 0;
			while(j<2*i+1){
				if(j==0||j==2*i){
					System.out.print("*");
					j++;				
				}else{
					System.out.print(" ");	
					j++;
				}

			}
			i++;
			System.out.println();
		}
		int y = 0;
		while(y<3){
			int k =0;
			while(k<=y){
				System.out.print(" ");
				k++;
			}
			int j = 0;
			while(j<5-2*y){
				if(j==0||j==4-2*y){
					System.out.print("*");
					j++;
				}else{
					System.out.print(" ");	
					j++;
				}
				
			}
			y++;
			System.out.println();
		}
	}
}
  • 11
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值