java基础练习,通过入口函数随机一个N值,打印出一个相应的菱形
思路清晰,画图即可。
对了,方法多种,下列非最优解,欢迎探讨。
1、CreateStar 类,用于创建菱形package practice.print.star; import com.savvy.wit.web.commons.util.DateUtil; import practice.print.star.business.Star; import java.util.*; public class CreateStar { private static int xLength = 0 ; //行高度 private static int yLength = 0 ; //列高度 private static Map< Integer,List > map = new HashMap< Integer,List >(); // 存 private int num = 0; //打印多少行 private boolean odevity = false; //默认奇数 public CreateStar() { // System.out.println(DateUtil.getNow("yyyy-MM-dd HH:mm:ss")+"\t===>\t"+this.getClass().getName()+":constructor" ); } public CreateStar(int num) { System.out.println(DateUtil.getNow("yyyy-MM-dd HH:mm:ss")+"\t===>\t"+this.getClass().getName()+":constructor" ); this.num = num; this.odevity = num % 2 == 0 ? true : false; this.xLength = num % 2 != 0 ? num : (num-1); this.yLength = num; } public void setnum(int num) { this.num = num; } public Map> getStars() { if(yLength == 0 && num != 0) { new CreateStar(this.num); } int start = startIndex(xLength); // 起始点 int invert; //反转偏移 for ( int y =0; y < yLength; y ++ ) { List list = new ArrayList(); int index = 0; //当前游标 if(this.odevity) invert = 2*y >= xLength ? (xLength - y) : 0; //奇偶性决定反转游标偏移量 else invert = 2*y >= xLength ? (xLength -1 - y) : 0; for( int x = 0; x < xLength; x++ ) { index ++; Star star = new Star(); star.setId(UUID.randomUUID().toString().replaceAll("-"," ")); if(2*y >= xLength) { //反转 if( index >= (start - invert) && index <= (start + invert) ) { star.setCode(1); }else { star.setCode(0); } }else if( index >= (start - y) && index <= (start + y) ) { star.setCode(1); }else { star.setCode(0); } list.add(star); } map.put(y,list); } return map; } /** * 起始点坐标 * @param width * @return */ private static int startIndex( int width ) { return width%2 ==0 ? width/2 : width/2+1; } }
2、PrintStars 类,用于打印菱形package practice.print; import com.savvy.wit.web.commons.util.DateUtil; import practice.print.star.CreateStar; import practice.print.star.business.Star; import java.util.*; public class PrintStars { private static int height = 0; public static void main(String[] args) { height = Integer.parseInt( String.valueOf(new Date().getTime()).substring(11,12) ) ; // CreateStar createStar = new CreateStar(); // createStar.setnum(height < 1 ? 1 : height); // new PrintStars().printStarsMap(createStar.getStars()); System.out.println(height); new PrintStars().printStarsMap ( new CreateStar(height < 1 ? 1 : height).getStars() ); } public void printStarsMap (Map> map) { System.out.println(DateUtil.getNow("yyyy-MM-dd HH:mm:ss")+"\t===>\t"+this.getClass().getName()); for (int h=0; h < (height == 0 ? 1 : height); h++) { for(Star star : map.get(h)) { System.out.print(star.getStar()); } System.out.println(); // Iterator iterator = map.get(h).iterator(); // while(iterator.hasNext()) { // if(iterator.next() instanceof Star) { // Star star = (Star) iterator.next(); // System.out.println( star.getStar() ); // } // } } } }
3、Star 类,打印格式package practice.print.star.business; import java.io.Serializable; public class Star implements Serializable { private static final String STAR = "☆"; private String id; // 0 空格 1 ☆ private int code; public String getId() { return id; } public void setId(String id) { this.id = id; } public void setCode(int code) { this.code = code; } public String getStar () { return code == 0 ? " " : STAR ; } public String getStar (String star) { if( code == 0) { StringBuilder sb = new StringBuilder(""); for (int i = 0; i < star.length(); i++) { sb.append(" "); } return sb.toString(); }else { return star; } } }
4、DateUtil,常用时间处理工具类
结果图