黑马程序员技术交流社区

标题: 打印一棵松树的代码,大神们看看有没有更简便的方法 [打印本页]

作者: 知足、遗忘    时间: 2015-9-10 09:28
标题: 打印一棵松树的代码,大神们看看有没有更简便的方法
  1. package com.sf;

  2. import java.util.*;

  3. public class test5 {
  4.         public static void main(String[] args) {
  5.                
  6.                 System.out.println("请输入第一个三角形的腰");
  7.                
  8.                 Scanner sc = new Scanner(System.in);
  9.                 int length1 = sc.nextInt();
  10.                
  11.                 System.out.println("请输入第二个三角形的腰");
  12.                 int length2 = sc.nextInt();
  13.                
  14.                 System.out.println("请输入矩形的长");
  15.                 int length3 = sc.nextInt();
  16.                 triangle(length1);
  17.                 triangle(length2);
  18.                 rectangle(length3);
  19.                 sc.close();
  20.         }
  21.         private static void triangle(int length){
  22.                 for(int x=0; x<length; x++){
  23.                         printKong(x);
  24.                         for(int z=0;z<x+1;z++){
  25.                                 System.out.print("*");
  26.                         }
  27.                         for(int z=0;z<x;z++){
  28.                                 System.out.print("*");
  29.                         }
  30.                         System.out.println();
  31.                 }
  32.         }
  33.         private static void rectangle(int length){
  34.                 for(int x=0; x<length; x++){
  35.                         printKong(1);
  36.                         for(int y=0; y<3;y++){
  37.                                 System.out.print("*");
  38.                         }
  39.                         System.out.println();
  40.                 }
  41.                
  42.         }
  43.         private static void printKong(int x){
  44.                 for(int y=0; y<16-x;y++){
  45.                        
  46.                         System.out.print(" ");
  47.                 }
  48.                
  49.         }

  50. }
复制代码

f:\\1.png
作者: ln0491    时间: 2015-9-10 09:30
学习了,挺 好玩的。。。。。。。。。
作者: 纳木错的程序猿    时间: 2015-9-10 09:38
学习一下,还没有学习到输入这一块。




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