java基础练习,通过入口函数随机一个N值,打印出一个相应的菱形 思路清晰,画图即可。 对了,方法多种,下列非最优解,欢迎探讨。 1、CreateStar 类,用于创建菱形 [java] view plain copy
- 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<Star> > map = new HashMap< Integer,List<Star> >(); // 存 private int num = 0; //打印多少行 private boolean odevity = false; //默认奇数 public CreateStar() { // System.out.println(DateUtil.getNow()+"\t===>\t"+Thread.currentThread().getStackTrace()[1].getClassName()+":"+Thread.currentThread().getStackTrace()[1].getMethodName()); } public CreateStar(int num) { System.out.println(DateUtil.getNow("yyyy-MM-dd HH:mm:ss")+"\t===>\t"+this.getClass().getName()+":"+Thread.currentThread().getStackTrace()[1].getMethodName()); 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<Integer,List<Star>> getStars() { System.out.println(DateUtil.getNow()+"\t===>\t"+Thread.currentThread().getStackTrace()[1].getClassName()+":"+Thread.currentThread().getStackTrace()[1].getMethodName()); 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 ) { System.out.println(DateUtil.getNow()+"\t===>\t"+Thread.currentThread().getStackTrace()[1].getClassName()+":"+Thread.currentThread().getStackTrace()[1].getMethodName()); return width%2 ==0 ? width/2 : width/2+1; } public void printStarsMap (Map<Integer,List<Star>> map) { System.out.println(DateUtil.getNow("yyyy-MM-dd HH:mm:ss")+"\t===>\t"+this.getClass().getName()+":"+Thread.currentThread().getStackTrace()[1].getMethodName()); for (int h=0; h < (num == 0 ? map.size() : num); h++) { map.get(h).forEach( star -> System.out.print(star.getStar() )); System.out.println(); } } public void printStarsMap (Map<Integer,List<Star>> map,String sign) { System.out.println(DateUtil.getNow("yyyy-MM-dd HH:mm:ss")+"\t===>\t"+this.getClass().getName()+":"+Thread.currentThread().getStackTrace()[1].getMethodName()); for (int h=0; h < (num == 0 ? map.size() : num); h++) { map.get(h).forEach( star -> System.out.print(star.getStar(sign) )); System.out.println(); } } public void printStarsMap (String sign) { System.out.println(DateUtil.getNow("yyyy-MM-dd HH:mm:ss")+"\t===>\t"+this.getClass().getName()+":"+Thread.currentThread().getStackTrace()[1].getMethodName()); map = map.size()<=0 ? this.getStars() : map; for (int h=0; h < (num == 0 ? map.size() : num); h++) { map.get(h).forEach( star -> System.out.print(star.getStar(sign) )); System.out.println(); } }}
2、PrintStars 类,用于打印菱形 [java] view plain copy
- 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)throws Exception { 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 CreateStar(StringUtil.random(20)).printStarsMap("&*"); CreateStar.class.newInstance().printStarsMap( new CreateStar(height < 1 ? 1 : height).getStars() ); }}
3、Star 类,打印格式 [java] view plain copy
- 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,常用时间处理工具类
package com.savvy.wit.web.commons.util;import java.text.ParseException;import java.text.SimpleDateFormat;import java.util.Calendar;import java.util.Date;import java.util.Random;/** * Created by aj907 on 2018/4/27. */public class DateUtil { private static Random random = new Random(); private static String pool = "qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM" ; /** * 根据系统时间随机获取一个编号 * @param len* @return*/public static Long createNumber(int len){ StringBuilder sb = new StringBuilder(getNow("yyyyMMddHHmmss")); for(int i = 0 ; i++<len ;){ sb.append(random(10)); } return Long.parseLong(sb.toString()); } /** * 根据系统时间随机获取一个编号 * @param len* @return*/public static String createCode(int len){ StringBuilder sb = new StringBuilder(getNow("yyyyMMddHHmmss")); for(int i = 0 ; i++<len ;){ sb.append( pool.charAt(random(pool.length()) )); } return sb.toString(); } /** * 获取处理耗时 * @param start* @param end* @return*/public static String getTimeConsuming( long start,long end){ long time=end-start;//计算插入耗时 return "耗时:\t"+(time>10000?getTime(time, "mm分ss秒"):time+"毫秒"); } /** * 获取当前系统时间 * @param format 日期转化格式 * 建议格式:yyyy-MM-dd HH:mm:ss * @return String 日期 */public static String getNow(String format){ return new SimpleDateFormat(format).format(new Date()); } public static String getNow(){ return new SimpleDateFormat("yyyy-MM-dd HH:mm:ss").format(new Date()); } /** * 毫秒值转为 format类型的时间字符 * @param time* @param format* @return*/public static String getTime(long time,String format){ Calendar calendar=Calendar.getInstance(); calendar.setTimeInMillis(time); return new SimpleDateFormat(format).format(calendar.getTime()); } /** * 获取系统当前毫秒值 * @return*/public static long getTime(){ return System.currentTimeMillis(); } /** * 获取时间 * * @param str 字符串日期格式 2017-06-26 13:21:12 * 统一解析格式 yyyy-MM-dd HH:mm:ss * @return date */public static Date getDate(String str){ String format="yyyy-MM-dd HH:mm:ss"; SimpleDateFormat sdf=new SimpleDateFormat(format); Date date=null; try { date=sdf.parse(str); } catch (ParseException e) { e.printStackTrace(); } return date; } /** * 获取随机数 * @param ran* @return*/public static int random(int ran){ return random.nextInt(ran == 0 ? 1 : ran ); }}5.StringUtil
public class StringUtil extends DateUtil { private static final String POOL = "qwertyuiopasdfghjklzxcvbnm"; /** * 获取字符串子串t在母串中的下标 * @param s 母串 * @param t 子串 * @return 存在则返回第一次查到的下标 查不到则返回-1 */public static int getIndex(String s,String t){ int length=t.length(); char c=t.charAt(0); for(int i=0;i<s.length();){ if(c==s.charAt(i)){ for(int j=1;;j++){ char sc=s.charAt(++i); char st=t.charAt(j); if(sc==st){ if(j<length-1) continue; else return i-j; }else{ i-=j-1; break; } } }else{ i++; } } return -1; } public static boolean isBlank(String string) { return string == null ? false : string == "" ? false : true; } public static boolean isNotBlank(String string) { return !isBlank(string); } public static String createCode() { StringBuilder sb = new StringBuilder(); for(int i = 0; i<random(Integer.parseInt(String.valueOf(System.currentTimeMillis()).substring(12,13))); i++) { sb.append( random(2)%2 == 0 ? POOL.charAt(random(POOL.length())) : String.valueOf(POOL.charAt(random(POOL.length()))).toUpperCase() ); } return sb.toString(); }}结果图
【转载】原文地址:https://blog.csdn.net/csdn277/article/details/80682149
|
|