数组——在java编程中制作一个月专用日历的有效方法

ki0zmccv  于 2021-08-20  发布在  Java
关注(0)|答案(3)|浏览(459)

我需要一些帮助;我被这种情况困扰了一段时间。我的目标是显示2021年8月的日历表单。我只需要8月份,因为我只想发布一个月的日历,供用户查看开课日期。它显示日历,但我觉得它有一种更有效的方式来编写这种类型的程序,但我不知道如何编写。你能帮帮我吗?
这是我的密码:

  1. public class Calendar {
  2. /**
  3. * @param args the command line arguments
  4. */
  5. public static void main(String[] args) {
  6. System.out.println("\t\tMONTH OF AUGUST 2021");
  7. System.out.println("–––––––––––––––-------------------------––––––––––––––");
  8. System.out.println("\nSun\tMon\tTue\tWed\tThu\tFri\tSat\n");
  9. System.out.println("–––––––––––––––-------------------------––––––––––––––");
  10. int [][]num={{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,1,2,3,4}};
  11. for (int i=1; i<num.length;i++){
  12. for(int j=8; j<num[i].length; j++){
  13. num[i][j]=i+j;
  14. }
  15. }
  16. for(int[] a: num){
  17. for(int i:a){
  18. System.out.print(i + "\t");
  19. }
  20. System.out.println("\n");
  21. }
  22. }
  23. }
6tdlim6h

6tdlim6h1#

要在控制台窗口中显示日历(无论哪个月),可以使用以下java方法:

  1. public static void displayConsoleCalendar(int day, int month, int year,
  2. boolean... useColorCodes) {
  3. boolean useColor = false;
  4. if (useColorCodes.length > 0) {
  5. useColor = useColorCodes[0];
  6. }
  7. String red = "\033[0;31m";
  8. String blue = "\033[0;34m";
  9. String reset = "\033[0m";
  10. java.util.Calendar calendar = new java.util.GregorianCalendar(year, month - 1, day + 1);
  11. calendar.set(java.util.Calendar.DAY_OF_MONTH, 1); //Set the day of month to 1
  12. int dayOfWeek = calendar.get(java.util.Calendar.DAY_OF_WEEK); //get day of week for 1st of month
  13. int daysInMonth = calendar.getActualMaximum(java.util.Calendar.DAY_OF_MONTH);
  14. //print month name and year
  15. System.out.println(new java.text.SimpleDateFormat("MMMM YYYY").format(calendar.getTime()));
  16. System.out.println((useColor?blue:"") + " S M T W T F S" + (useColor?reset:""));
  17. //print initial spaces
  18. String initialSpace = "";
  19. for (int i = 0; i < dayOfWeek - 1; i++) {
  20. initialSpace += " ";
  21. }
  22. System.out.print(initialSpace);
  23. //print the days of the month starting from 1
  24. for (int i = 0, dayOfMonth = 1; dayOfMonth <= daysInMonth; i++) {
  25. for (int j = ((i == 0) ? dayOfWeek - 1 : 0); j < 7 && (dayOfMonth <= daysInMonth); j++) {
  26. if (dayOfMonth == day && useColor) {
  27. System.out.printf(red + "%2d " + reset, dayOfMonth);
  28. }
  29. else {
  30. System.out.printf("%2d ", dayOfMonth);
  31. }
  32. dayOfMonth++;
  33. }
  34. System.out.println();
  35. }
  36. }

如果您的终端支持转义颜色代码,则日历中您提供的日期将为红色。这种方法还考虑了闰年。只需提供日、月和年的整数值作为参数。应用颜色代码的最后一个参数是可选的。
如果您希望显示8月份,并希望该月份的20号以红色突出显示,则可以拨打以下电话:

  1. displayConsoleCalendar(20, 8, 2021, true);

控制台窗口可能会显示如下内容:

展开查看全部
klr1opcd

klr1opcd2#

要提出此解决方案(线性时间复杂度):
使用运行for循环 i 从…起 0 to 34 仅在以下情况下打印新行: i != 0 && i % 7 == 0 使用 i % 31 + 1 关于当前的数字 i 把它限制在一定范围内

  1. public static void main(String[] args) {
  2. System.out.println("\t\tMONTH OF AUGUST 2021");
  3. System.out.println("–––––––––––––––-------------------------––––––––––––––");
  4. System.out.println("\nSun\tMon\tTue\tWed\tThu\tFri\tSat\n");
  5. System.out.println("–––––––––––––––-------------------------––––––––––––––");
  6. for(int i = 0; i < 35; i++) {
  7. if(i!= 0 && i % 7 == 0)
  8. System.out.println("\n");
  9. System.out.print(i % 31 + 1 + "\t");
  10. }
  11. }

这有一个复杂而简短的版本(供好奇的人使用)

  1. while(i < 35)
  2. System.out.print(
  3. ((i != 0 && i % 7 == 0) ? "\n": "")
  4. + (i++ % 31 + 1)
  5. + "\t"
  6. );
展开查看全部
icomxhvb

icomxhvb3#

这里有一个替代方案,它实际上并不更加优雅或高效,但使用了不同的方法(不同的库),并且支持不同的语言:

  1. public static void printCalendarFor(int month, int year, Locale locale) {
  2. // build the given month of the given year
  3. YearMonth yearMonth = YearMonth.of(year, month);
  4. // extract its name in the given locale
  5. String monthName = yearMonth.getMonth().getDisplayName(TextStyle.FULL, locale);
  6. // create a builder for the calendar
  7. StringBuilder calendarBuilder = new StringBuilder();
  8. // and append a header with the month name and the year
  9. calendarBuilder.append("\t\t ").append(monthName).append(" ").append(year).append(System.lineSeparator());
  10. // then append a second header with all the days of week
  11. calendarBuilder.append(DayOfWeek.MONDAY.getDisplayName(TextStyle.SHORT, locale)).append("\t")
  12. .append(DayOfWeek.TUESDAY.getDisplayName(TextStyle.SHORT, locale)).append("\t")
  13. .append(DayOfWeek.WEDNESDAY.getDisplayName(TextStyle.SHORT, locale)).append("\t")
  14. .append(DayOfWeek.THURSDAY.getDisplayName(TextStyle.SHORT, locale)).append("\t")
  15. .append(DayOfWeek.FRIDAY.getDisplayName(TextStyle.SHORT, locale)).append("\t")
  16. .append(DayOfWeek.SATURDAY.getDisplayName(TextStyle.SHORT, locale)).append("\t")
  17. .append(DayOfWeek.SUNDAY.getDisplayName(TextStyle.SHORT, locale)).append(System.lineSeparator());
  18. // get the first day of the given month
  19. LocalDate dayOfMonth = yearMonth.atDay(1);
  20. // and go through all the days of the month handling each day
  21. while (!dayOfMonth.isAfter(yearMonth.atEndOfMonth())) {
  22. // in order to fill up the weekday table's first line, specially handle the first
  23. if (dayOfMonth.getDayOfMonth() == 1) {
  24. switch (dayOfMonth.getDayOfWeek()) {
  25. case MONDAY:
  26. calendarBuilder.append(String.format("%3d", dayOfMonth.getDayOfMonth()))
  27. .append("\t");
  28. break;
  29. case TUESDAY:
  30. calendarBuilder.append("\t")
  31. .append(String.format("%3d", dayOfMonth.getDayOfMonth()))
  32. .append("\t");
  33. break;
  34. case WEDNESDAY:
  35. calendarBuilder.append("\t\t")
  36. .append(String.format("%3d", dayOfMonth.getDayOfMonth()))
  37. .append("\t");
  38. break;
  39. case THURSDAY:
  40. calendarBuilder.append("\t\t\t")
  41. .append(String.format("%3d", dayOfMonth.getDayOfMonth()))
  42. .append("\t");
  43. break;
  44. case FRIDAY:
  45. calendarBuilder.append("\t\t\t\t")
  46. .append(String.format("%3d", dayOfMonth.getDayOfMonth()))
  47. .append("\t");
  48. break;
  49. case SATURDAY:
  50. calendarBuilder.append("\t\t\t\t\t")
  51. .append(String.format("%3d", dayOfMonth.getDayOfMonth()))
  52. .append("\t");
  53. break;
  54. case SUNDAY:
  55. calendarBuilder.append("\t\t\t\t\t\t")
  56. .append(String.format("%3d", dayOfMonth.getDayOfMonth()))
  57. .append(System.lineSeparator());
  58. break;
  59. }
  60. } else {
  61. // for all other days, just append their numbers
  62. calendarBuilder.append(String.format("%3d", dayOfMonth.getDayOfMonth()));
  63. // end the line on Sunday, otherwise append a tabulator
  64. if (dayOfMonth.getDayOfWeek() == DayOfWeek.SUNDAY) {
  65. calendarBuilder.append(System.lineSeparator());
  66. } else {
  67. calendarBuilder.append("\t");
  68. }
  69. }
  70. // finally increment the day of month
  71. dayOfMonth = dayOfMonth.plusDays(1);
  72. }
  73. // print the calender
  74. System.out.println(calendarBuilder.toString());
  75. }

下面是一些使用示例

  1. public static void main(String[] args) {
  2. printCalendarFor(8, 2021, Locale.ENGLISH);
  3. }

该示例使用的输出是

  1. August 2021
  2. Mon Tue Wed Thu Fri Sat Sun
  3. 1
  4. 2 3 4 5 6 7 8
  5. 9 10 11 12 13 14 15
  6. 16 17 18 19 20 21 22
  7. 23 24 25 26 27 28 29
  8. 30 31
展开查看全部

相关问题