用来查询农历年的代码,可能比较粗糙,希望不要太打击偶,哈哈。
一,Console.java[用来获取控制台的输入]
/*
#(@)Console.java20:40:252/2/
控制台消息输入/输出。
*/
importjava.io.*;
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
publicfinalclassConsole{
publicConsole(){}
privatestaticSimpleDateFormatstyle=newSimpleDateFormat("yyyy-MM-ddHH:mm:ss");
/**
*根据提示返回用户的输入
*
*@paramsString控制台输入提示
*@returnString返回用户的控制台输入
*/
publicStringread(Stringm){
Strings=null;
System.out.println(m);
try{
BufferedReaderin=newBufferedReader(newInputStreamReader(System.in));
s=in.readLine();
}catch(IOExceptionex){
printError(this.getClass().getName(),"read(Strings)",ex.getMessage());
}
returns;
}
/**
*根据提示返回用户的输入
*
*@paramsString控制台输入提示
*@returnString返回用户的控制台输入
*/
publicintreadInt(Strings){
intx;
try{
x=Integer.parseInt(this.read(s));
}catch(NumberFormatExceptionex){
x=0;
printError(this.getClass().getName(),"readInt(Strings)",ex.getMessage());
}
returnx;
}
/**
*根据提示返回用户的输入
*
*@paramsString控制台输入提示
*@returnString返回用户的控制台输入
*/
publicDoublereadDouble(Strings){
doublex;
try{
x=Double.parseDouble(this.read(s));
}catch(NumberFormatExceptionex){
x=0;
printError(this.getClass().getName(),"readInt(Strings)",ex.getMessage());
}
returnx;
}
/**
*打印错误消息
*
*@paramcString类名
*@parammString方法名
*@parameString错误信息
*/
publicvoidprintError(Stringc,Stringm,Stringe){
System.out.println(style.format(Calendar.getInstance().getTime())+":调用"+c+"."+m+"方法时发生错误:"+e+"。");
}
/**
*main(String[]args)方法
*/
publicstaticvoidmain(String[]args){
Consolec=newConsole();
System.out.println(c.readDouble("请输入Double类型的数字……"));
}
}
二,Chinese.java[计算农历年的代码]
/*
#(@)Chinese.java21:56:252/2/
中国农历。干支纪年始于公元四年,即第一个甲子年,故甲、子位于数组中的第四位。
*/
importjava.text.SimpleDateFormat;
importjava.util.Calendar;
publicfinalclassChinese{
publicChinese(){}
privateConsolec=newConsole();
/*天干数组*/
privateString[]s={"庚","辛","壬","癸","甲","乙","丙","丁","戊","己"};
/*地支数组*/
privateString[]t={"申","酉","戌","亥","子","丑","寅","卯","辰","巳","午","未"};
/*十二生肖*/
privateString[]g={"猴","鸡","狗","猪","鼠","牛","虎","兔","龙","蛇","马","羊"};
privateStringagain=null;
/**
*根据用户的输入确定继续查询或退出运行
*
*/
publicvoidchinese(){
if(this.chineseera()){
Strings=c.read("提示:按回车键退出,重新查询请输入新的年数后按回车键。");
if(s!=null&&!s.trim().equals("")){
again=s;
this.chinese();
}else{
System.exit(0);
}
}
}
/**
*根据用户的输入查询农历年和生肖属相,如果没有用户输入,则按当前年度开始查询
*
*/
privatebooleanchineseera(){
booleanb=false;
Stringyear=null;
if(again==null){
year=c.read("请输入准备查询农历年的年数,完成后按回车键。");
if(year==null||year.equals("")){
SimpleDateFormatstyle=newSimpleDateFormat("yyyy");
year=style.format(Calendar.getInstance().getTime());
}
}else{
year=again;
}
inti=this.last(year);
if(i!=-1){
inty=this.cathay(year);
System.out.println(year+"年是农历"+s[i]+t[y]+"年,十二生肖中属"+g[y]+"。");
b=true;
}
returnb;
}
/**
*取得字符串的最后一位字符并转换成数字型变量
*取得天干时的算法,主要是用最后一位数字对应天干数组中的数位字符
*
*@paramsString用户输入的年数
*@returnint返回转换后的最后一位数字,如果发生错误,则返回-1
*/
privateintlast(Strings){
inty=0;
if(s!=null){
try{
s=s.substring(s.length()-1,s.length());
y=Integer.parseInt(s);
}catch(NumberFormatExceptionex){
y=-1;
c.printError(this.getClass().getName(),"last(Strings)",ex.getMessage());
}
}
returny;
}
/**
*取得字符串除以十二以后的余数
*取得地支时的算法,主要是用除以十二以后的余数对应地支数组中的数位字符
*十二生肖的算法也与此相同
*
*@paramsString用户输入的年数
*@returnint返回除以十二以后的余数
*/
privateintcathay(Strings){
inty=0;
try{
y=Integer.parseInt(s);
y%=12;
}catch(NumberFormatExceptionex){
c.printError(this.getClass().getName(),"cathay(Strings)",ex.getMessage());
}
returny;
}
/**
*main(String[]args)方法
*/
publicstaticvoidmain(String[]args){
Chinesecathay=newChinese();
cathay.chinese();
}
}