+ -
当前位置:首页 → 问答吧 → java print calendar

java print calendar

时间:2014-01-02

来源:互联网

引用:public class Month {
final static int DAYS_PER_WEEK = 7;
final static String[] DAYS = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
int days; //number of days in a month
int firstDay; //specifies the first day of a month

Month(int myDays, int myFirstDay){
days = myDays;
firstDay = myFirstDay;
}

void printCalendar(){
for(int i=0; i<days_per_week; i++)
System.out.print(DAYS + "\t");
System.out.println("");
System.out.print("\t" + "\t" + "\t");

for (int i=1; i<5; i++)
System.out.print(i + "\t");
System.out.println("");

for (int i=5; i<12; i++)
System.out.print(i + "\t");
System.out.println("");

for (int i=12; i<19; i++)
System.out.print(i + "\t");
System.out.println("");

for (int i=19; i<26; i++)
System.out.print(i + "\t");
System.out.println("");

for (int i=26; i<32; i++)
System.out.print(i + "\t");
System.out.println("");

}

}


OUTPUT:
Sun Mon Tue Wed Thu Fri Sat
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

under the method called printCalendar(),我用左for loop去print每个星期的日子
但系每一个月都咁样用for loop,成个program会好长
我想问有冇简单d既方法去print the calendar

[ 本帖最后由 winnywingy 於 2013-11-17 10:46 PM 编辑 ]

作者: winnywingy   发布时间: 2014-01-02

复制内容到剪贴板代码:public class Month { final static int DAYS_PER_WEEK = 7; final static String[] DAYS = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; int days; //number of days in a month int firstDay; //specifies the first day of a month Month(int myDays, int myFirstDay){ assert(1<=myDays && myDays<= 31 ) : "Invalid myDays: " + myDays; assert(1<=myFirstDay && myFirstDay <= 7) : "Invalid myFirstDay " + myFirstDay;
days = myDays; firstDay = myFirstDay; }
public String toString(){ StringBuilder strBuilder = new StringBuilder(); for(int i=0; i<DAYS_PER_WEEK; i++) strBuilder.append(String.format("%s\t", DAYS[ i ])); strBuilder.append("\n");

for(int i=1; i<firstDay + days; i++) { if (i < firstDay) { strBuilder.append("\t"); } else if (i%7 == 0) { strBuilder.append(String.format("%s\n", i - firstDay + 1)); } else { strBuilder.append(String.format("%s\t", i - firstDay + 1)); } } return strBuilder.append("\n").toString(); } public static void main(String[] args) { Month month = new Month(31, 3); System.out.println(month); }}
[ 本帖最后由 dsekid 於 2013-11-19 09:33 PM 编辑 ]

作者: dsekid   发布时间: 2014-01-02

it seems discuss doesn't work well with chrome browser
复制内容到剪贴板代码:public class Month {
final static int DAYS_PER_WEEK = 7;
final static String[] DAYS = {"Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"};
int days; //number of days in a month
int firstDay; //specifies the first day of a month

Month(int myDays, int myFirstDay){
assert(28<=myDays && myDays<= 31 ) : "Invalid myDays: " + myDays;
assert(1<=myFirstDay && myFirstDay <= 7) : "Invalid myFirstDay " + myFirstDay;

days = myDays;
firstDay = myFirstDay;
}

public String toString(){
StringBuilder strBuilder = new StringBuilder();
for(int i=0; i<DAYS_PER_WEEK; i++)
strBuilder.append(String.format("%s\t", DAYS[ i ]));
strBuilder.append("\n");


for(int i=1; i<firstDay + days; i++) {
if (i < firstDay) {
strBuilder.append("\t");
} else if (i%7 == 0) {
strBuilder.append(String.format("%s\n", i - firstDay + 1));
} else {
strBuilder.append(String.format("%s\t", i - firstDay + 1));
}
}
return strBuilder.append("\n").toString();
}

public static void main(String[] args) {
Month month = new Month(31, 3);
System.out.println(month);
}
}
[ 本帖最后由 dsekid 於 2013-11-19 09:57 PM 编辑 ]

作者: dsekid   发布时间: 2014-01-02

thank you ar
but we have to use the method called printCalendar() to print out the calendar
do you know another way to do it?

作者: winnywingy   发布时间: 2014-01-02

热门下载

更多