所以我有两个月的数组存储事件时间,日期和优先级:
Events[] september= new Events[30];
Events[] october = new Events[31];
我想在其中插入我的日期数组,并使它们以特定格式显示。
static String[] days = {"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"};
如何获得这样的输出?我似乎找不到自己的方式:
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
我为它写了两个不同的代码我的代码是:
public static void DisplayEvents(Events[] event,int firstWeekday) {
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for(int i =0; i<days.length;i++) {
System.out.println(String.format("%1$5s", days[i]) +"");
}
和
public static void DisplayEvents(Events[] event,int firstWeekday) {
int weekDayIndex =0;
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
for(int day=1; day<days.length; day++){
System.out.println(" ");
weekDayIndex++;
}
for(int day =1; day<event.length;day++){
System.out.print(String.format("%1$5s", day));
weekDayIndex++;
if (weekDayIndex== 7){
weekDayIndex = 0;
System.out.println();
} else {
System.out.println("");
}
}
}
输出这些代码给我的是:
更新输出:
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
星号表示举行活动的日子。我已经写了一个代码来放入随机事件,我有我的星号。我的问题是只是以日历格式打印它,并能够在有事件的日子里放置我的星号。任何贡献/帮助将不胜感激。如果需要,我可以提供更多信息。
答案 0 :(得分:0)
在这一行
System.out.println(String.format("%1$5s", day));
使用print()
代替println()
。
答案 1 :(得分:0)
从概念上讲,您希望将每一行构建为单个String
或输出,在打印新行之前填充所有列。
“主要”原因是在System.out.println(String.format("%1$5s", day));
循环中使用for(int day =1; day<event.length;day++){
,每天在新行上打印出来。
现在,您可以通过多种方式实现此目标,例如,您可以使用System.out.print
代替System.out.println
,或者您可以在所有7天内使用预定义格式({ {1}})并填写
或者您可以使用类似"%2s%1s %2s%1s %2s%1s %2s%1s %2s%1s %2s%1s %2s%1s"
的内容,它可以代表一行,生成给定日期的输出并将其附加到StringBuilder
,例如......
StringBuilder
打印....
LocalDate date = LocalDate.of(2015, Month.OCTOBER, 1);
int month = date.getMonthValue();
LocalDate endOfMonth = date.withDayOfMonth(date.lengthOfMonth()).plusDays(1);
while (date.getDayOfWeek() != DayOfWeek.SUNDAY) {
date = date.minusDays(1);
}
String format = "%2s%1s ";
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
while (date.isBefore(endOfMonth)) {
StringBuilder row = new StringBuilder(7 * 4);
for (int index = 0; index < 7; index++) {
if (month == date.getMonthValue()) {
row.append(String.format(format, Integer.toString(date.getDayOfMonth()), ""));
} else {
row.append(String.format(format, "", ""));
}
date = date.plusDays(1);
}
System.out.println(row);
}
因为我有十分喜欢,十一月......
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
现在,显然,我没有实现你的“事件”逻辑,但是我为它留下了格式(我只是在示例中将其留空)
答案 2 :(得分:0)
你打印好标题。下一步打印出空格。
int currentWeekDay = 0;
for(int i = 0; i<firstWeekDay; i++){
System.out.print(" ");
currentWeekDay++;
//just in case the firstWeekDay is invalid.
if(currentWeekDay==7){
currentWeekDay=0;
System.out.println();
}
}
现在打印出数字并每次都添加一个硬回车。
for(int i = 0; i<event.length; i++){
System.out.printf("%4s", event[i]);
currentWeekDay++;
if(currentWeekDay==7){
currentWeekDay=0;
System.out.println();
}
}
请注意,我使用您创建的字符串数组而不是格式化您正在迭代的索引。
答案 3 :(得分:0)
public static void DisplayEvents(Events[] event, int firstWeekday) {
int weekDayIndex =0;
System.out.println("Sun Mon Tue Wed Thu Fri Sat");
System.out.print("1");
weekDayIndex++;
for(int day = 2; day < event.length;day++){
if(weekDayIndex== 7){
weekDayIndex = 0;
System.out.println();
System.out.print(day);
weekDayIndex++;
} else {
System.out.print(String.format("%1$4s", day));
weekDayIndex++;
}
}
}
打印:
星期二星期二星期三星期五星期五星期六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