程序员并不是没有一点乐趣整天枯燥无味的游走在代码中的,在这里给大家提供了一个表白代码
功能描述:
1:能够计算任意两个时间的之间相隔的天数,2:在相隔天数下面输出心形并且心形里面还有英文我爱你字样。
相信你已经迫不及待了,那就上效果图
废话不多说了,请看代码:
- 日期计算:(手动输入开始的日期和结束的日期 年 月 日)
事例:我输入的开始日期是2016年11月31日,结束日期是2019年3月5日。
//开始日期Calendar calendar = Calendar.getInstance();calendar.clear();;calendar.set(2016,11,31); //日期的填写//结束日期Calendar calendar1 = Calendar.getInstance();calendar1.clear();calendar1.set(2019,3,5);//天数的计算System.out.println(" "+(calendar1.getTimeInMillis() - calendar.getTimeInMillis())/86400000);
- 心形输出:(此处使用了callBack方法,需要对此方法调用显示心形)
public static String callBack(String input){int[] array = {0,1,0,0,0,1,0,1,0,1,0,1,0,1,1,0,0,1,0,0,1,1,4,5,2,3,4,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0};StringBuffer sb = new StringBuffer();for (int i=0;i<array.length;i++){if (i % 7 == 0)sb.append(" \n");if (array[i] == 0)sb.append(" ");else if (array[i] == 4)sb.append(" ");else if (array[i] == 5)sb.append(" I ");else if (array[i] == 2)sb.append("Lvoe ");else if (array[i] == 3)sb.append("You");elsesb.append(" " + input);}return sb.toString();}
- 对callBack方法的调用
public static void main(String[] args){//输出心形System.out.println(callBack("*"));}
至此已经能完整实现。
下面向大家展示一下整体代码即对上面步骤的汇总:
public class Time {public static void main(String[] args){//开始日期Calendar calendar = Calendar.getInstance();calendar.clear();;calendar.set(2016,11,31); //日期的填写//结束日期Calendar calendar1 = Calendar.getInstance();calendar1.clear();calendar1.set(2019,3,5);//天数的计算System.out.println(" "+(calendar1.getTimeInMillis() - calendar.getTimeInMillis())/86400000);//输出心形System.out.println(callBack("*"));}public static String callBack(String input){int[] array = {0,1,0,0,0,1,0,1,0,1,0,1,0,1,1,0,0,1,0,0,1,1,4,5,2,3,4,1,0,1,0,0,0,1,0,0,0,1,0,1,0,0,0,0,0,1,0,0,0,0,0,0,0,0};StringBuffer sb = new StringBuffer();for (int i=0;i<array.length;i++){if (i % 7 == 0)sb.append(" \n");if (array[i] == 0)sb.append(" ");else if (array[i] == 4)sb.append(" ");else if (array[i] == 5)sb.append(" I ");else if (array[i] == 2)sb.append("Lvoe ");else if (array[i] == 3)sb.append("You");elsesb.append(" " + input);}return sb.toString();}}
希望此代码能够对你有所帮助!