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("");
}
}
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 编辑 ] 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 发布时间: 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 编辑 ] 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 发布时间: 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?
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
相关阅读 更多
热门阅读
-
office 2019专业增强版最新2021版激活秘钥/序列号/激活码推荐 附激活工具
阅读:74
-
如何安装mysql8.0
阅读:31
-
Word快速设置标题样式步骤详解
阅读:28
-
20+道必知必会的Vue面试题(附答案解析)
阅读:37
-
HTML如何制作表单
阅读:22
-
百词斩可以改天数吗?当然可以,4个步骤轻松修改天数!
阅读:31
-
ET文件格式和XLS格式文件之间如何转化?
阅读:24
-
react和vue的区别及优缺点是什么
阅读:121
-
支付宝人脸识别如何关闭?
阅读:21
-
腾讯微云怎么修改照片或视频备份路径?
阅读:28