java程序关于日历代码的解读 15
请高手解读一下以下代码,帮忙注释一下importjava.io.BufferedReader;importjava.io.IOException;importjava.i...
请高手解读一下以下代码,帮忙注释一下
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ring {
public static void main(String args[]) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入一个年份:");
String years=in.readLine();
CalendarPrinter cp = new CalendarPrinter(years);
cp.printCal();
}
}
class CalendarPrinter{
private int year;
private static final int monthCount = 12;
public CalendarPrinter(String years){
year= Integer.parseInt(years);
}
public void printCal(){
GregorianCalendar gCal=new GregorianCalendar();
gCal.set(Calendar.YEAR,year);
for(int month=0;month<monthCount;month++){
System.out.println((month+1)+"月份");
gCal.set(Calendar.MONTH,month);
printOut(gCal);
}
}
private void printOut(Calendar cal){
int month=cal.get(Calendar.MONTH);
cal.set(Calendar.DAY_OF_MONTH,1);
int weekday=cal.get(Calendar.DAY_OF_WEEK);
System.out.println("Sun MOn Tue Wed Thu Fri Sat");
for (int i=Calendar.SUNDAY;i<weekday;i++)
System.out.print(" ");
do{
int day=cal.get(Calendar.DAY_OF_MONTH);
if(day<10)
System.out.print(" "+day+" ");
else
System.out.print(" "+day+" ");
if(weekday==Calendar.SATURDAY)
System.out.println();
cal.add(Calendar.DAY_OF_MONTH, 1);
weekday=cal.get(Calendar.DAY_OF_WEEK);
}while(cal.get(Calendar.MONTH)==month);
if(weekday!=Calendar.SUNDAY)
System.out.println();
}
} 展开
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.Calendar;
import java.util.GregorianCalendar;
public class ring {
public static void main(String args[]) throws IOException{
BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.print("请输入一个年份:");
String years=in.readLine();
CalendarPrinter cp = new CalendarPrinter(years);
cp.printCal();
}
}
class CalendarPrinter{
private int year;
private static final int monthCount = 12;
public CalendarPrinter(String years){
year= Integer.parseInt(years);
}
public void printCal(){
GregorianCalendar gCal=new GregorianCalendar();
gCal.set(Calendar.YEAR,year);
for(int month=0;month<monthCount;month++){
System.out.println((month+1)+"月份");
gCal.set(Calendar.MONTH,month);
printOut(gCal);
}
}
private void printOut(Calendar cal){
int month=cal.get(Calendar.MONTH);
cal.set(Calendar.DAY_OF_MONTH,1);
int weekday=cal.get(Calendar.DAY_OF_WEEK);
System.out.println("Sun MOn Tue Wed Thu Fri Sat");
for (int i=Calendar.SUNDAY;i<weekday;i++)
System.out.print(" ");
do{
int day=cal.get(Calendar.DAY_OF_MONTH);
if(day<10)
System.out.print(" "+day+" ");
else
System.out.print(" "+day+" ");
if(weekday==Calendar.SATURDAY)
System.out.println();
cal.add(Calendar.DAY_OF_MONTH, 1);
weekday=cal.get(Calendar.DAY_OF_WEEK);
}while(cal.get(Calendar.MONTH)==month);
if(weekday!=Calendar.SUNDAY)
System.out.println();
}
} 展开
1个回答
展开全部
类CalendarPrinter有两个主要的方法printCal()和printOut(),printCal()打印你输入年份的每一个月份,printOut()打印每一个月的每一天,在printCal()方法先创建一个格里高利历法的对象,并把你输入的年份设置为该历法的年份,然后for循环打印12个月份,然后再把每个月分别设置为你输入那年的每个月,接着调用printOut()方法打印每一天,在printOut()方法中,先获得月份,然后把日期设置为该月的第一天,接着在获取这月的第一天是一周中的周几weekday,然后打印出第一行“Sun MOn Tue Wed Thu Fri Sat”,接着就进入for循环,这个for循环就是打印每个月第一天开始前的空格,接着就进入do……while循环,获得天day,如果day<10,打印day的时候day前面的空格大一些,否则day前面的空格就小一些,为了排版好看,然后判断是否是周六,如果是周六就换行,然后把day+1,重新获得新的day在一周中是周几。循环一直下去,该月的天从1一直加到30或31,再加的话月份就该+1,所以do……while循环结束的条件就是进入到下一个月。最后判断该月的最后一天是不是该月的周日,如果不是就换行
追问
大神能不能在程序每行的后面注释一下,主要是关于日历的代码,谢谢
追答
...............................如果那样就发不过了,字数太多
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询