黑马程序员技术交流社区

标题: 打印倒三角形 [打印本页]

作者: 肚小糖    时间: 2015-8-23 00:31
标题: 打印倒三角形
  1. /*import java.util.Scanner;
  2. 打印出如下格式的三角形:
  3. * * * * *
  4. * * * *
  5.   * * *
  6.    * *
  7.     *  
  8. */
  9. public class ForDemo {
  10.     public static void main(String[] args)
  11.     {
  12.         Scanner input = new Scanner(System.in);
  13.         System.out.print("请输入三角形的行数: ");
  14.         int index = input.nextInt();
  15.         for (int x = 1; x <= index; x ++){
  16.             //首先打印出*前面的空格
  17.             for (int y = 1; y < x; y++)
  18.                 System.out.print(" ");
  19.             //再打印出*
  20.             for (int y = x; y <= index; y++)
  21.                 System.out.print("* ");
  22.             System.out.println();
  23.         }      
  24.     }
  25. }
复制代码





欢迎光临 黑马程序员技术交流社区 (http://bbs.itheima.com/) 黑马程序员IT技术论坛 X3.2