A股上市公司传智教育(股票代码 003032)旗下技术交流社区北京昌平校区

 找回密码
 加入黑马

QQ登录

只需一步,快速开始

© sandra_bae 中级黑马   /  2015-4-30 22:34  /  921 人查看  /  11 人回复  /   0 人收藏 转载请遵从CC协议 禁止商业使用本文

  1. /*
  2.         需求:封装一个函数,能够打印向上和向下的三角形
  3. */
  4. import java.util.Scanner;
  5. class PrintTriangle
  6. {
  7.         public static void printTriangle(int a)
  8.         {
  9.                 for(int x = 0;x < a; x++)
  10.                 {
  11.                         for(int y = 0;y <= x; y++)
  12.                         {
  13.                                 System.out.print("*");
  14.                         }
  15.                         System.out.println();
  16.                 }
  17.                 System.out.println("-----------------------");
  18.                 for(int x = 0;x < a; x++)
  19.                 {
  20.                         for(int y = x;y < a; y++)
  21.                         {
  22.                                 System.out.print("*");
  23.                         }
  24.                         System.out.println();
  25.                 }
  26.         }
  27.        

  28.         public static void main(String[] args)
  29.         {
  30.                 Scanner sc = new Scanner(System.in);

  31.                 System.out.println("想打几行的三角,随便打:");

  32.                 int a = sc.nextInt();

  33.                 printTriangle(a);
  34.         }

  35. }
复制代码


11 个回复

倒序浏览
不错//、加油////
回复 使用道具 举报
炫酷,加油!
回复 使用道具 举报
还可以哟
回复 使用道具 举报
不错 不错 学习了
回复 使用道具 举报
想法很好哦
回复 使用道具 举报 1 0

过奖,过奖~~
回复 使用道具 举报

还好啦~~
回复 使用道具 举报
rolan 发表于 2015-4-30 23:00
不错//、加油////

你的头像很酷炫~~
回复 使用道具 举报
不错,加油
回复 使用道具 举报
好好学习,天天向上
回复 使用道具 举报
不错,真有才!!
回复 使用道具 举报
您需要登录后才可以回帖 登录 | 加入黑马