根据用户输入打印菱形

public class For{

  

  1. public static void main(String[] args) {
  2.  
      int rows = 0; //菱形的行数
  3.  
      Scanner input = new Scanner(System.in);
  4.  
      System.out.print( "请输入菱形行数:");
  5.  
      rows = input.nextInt();
  6.  
      while(rows%2 == 0){
  7.  
       System.out.print( "请输入奇数:");
  8.  
    rows = input.nextInt();
  9.  
      }
  10.  
      int n = (rows+1)/2;
  11.  
      //打印菱形的上半部分
  12.  
      for(int i = 1; i <= n; i++){   //外层循环变量i控制行数
  13.  
       for(int j = 1; j <= n-i; j++){   //内层循环变量j控制该行空格数
  14.  
        System.out.print( " ");
  15.  
       }
  16.  
       for(int k = 1; k <= 2*i-1; k++){   //内层循环变量k控制该行*号数
  17.  
        System.out.print( "*");
  18.  
       }
  19.  
       System.out.print( "\n");
  20.  
      }
  21.  
      //打印菱形的下半部分
  22.  
      for(int i = n-1; i >= 1; i--){
  23.  
       for(int j = 1; j <= n-i; j++){
  24.  
        System.out.print( " ");
  25.  
       }
  26.  
       for(int k = 1; k <= 2*i-1; k++){
  27.  
        System.out.print( "*");
  28.  
       }
  29.  
       System.out.print( "\n");
  30.  
      }
  31.  
    }

}

转载于:https://www.cnblogs.com/loveyoulx/p/9582323.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值