Use Java to print the calendar of any year

Source: Internet
Author: User

 

Code:
  1. ImportJava. Io. bufferedreader;
  2. ImportJava. Io. ioexception;
  3. ImportJava. Io. inputstreamreader;
  4. Public ClassTextcontrol {
  5. Static IntYear, monthday, weekday; // defines static variables for other classes to call
  6. Public Static VoidMain (string [] ARGs)ThrowsIoexception
  7. {
  8. System. Out. Print ("enter a year :");
  9. Inputstreamreader IR; // The following six lines of code are accepted from the console.
  10. Bufferedreader in;
  11. IR =NewInputstreamreader (system. In );
  12. In =NewBufferedreader (IR );
  13. String S = in. Readline ();
  14. Year = integer. parseint (s );
  15. Weekday = firstday (year); // calculate the day of the week on the first day of the year.
  16. System. Out. println ("/N" + year + "year ");
  17. Printmonth ();
  18. }
  19. Public Static BooleanIsleapyear (IntY) // whether the rebellion is a leap year
  20. {
  21. Return(Y % 4 = 0 & Y % 100! = 0) | (Y % 400 = 0 ));
  22. }
  23. Public Static IntFirstday (IntY) // calculate the day of the week on the first day of the year.
  24. {
  25. LongN = y * 365;
  26. For(IntI = 1; I <Y; I ++)
  27. If(Isleapyear (I ))
  28. N + = 1;
  29. Return(Int) N % 7;
  30. }
  31. Public Static VoidPrintweek () // print the header
  32. {
  33. System. Out. println ("========================= ");
  34. System. Out. println ("May 25, 1234 ");
  35. }
  36. Public Static IntGetmonthday (IntM) // obtain the number of days per month
  37. {
  38. Switch(M)
  39. {
  40. Case1:
  41. Case3:
  42. Case5:
  43. Case7:
  44. Case8:
  45. Case10:
  46. Case12:Return31;
  47. Case4:
  48. Case6:
  49. Case9:
  50. Case11:Return30;
  51. Case2:
  52. If(Isleapyear (year ))Return29;
  53. Else Return28;
  54. Default:Return0;
  55. }
  56. }
  57. Public Static VoidPrintmonth () // print each month based on different conditions
  58. {
  59. For(IntM = 1; m <= 12; m ++) // Loop
  60. {
  61. System. Out. println (m + "month ");
  62. Printweek ();
  63. For(IntJ = 1; j <= weekday; j ++) // print the corresponding space based on the day of the week on the first day of each month.
  64. System. Out. Print ("");
  65. IntMonthday = getmonthday (m); // obtain the number of days per month
  66. For(IntD = 1; D <= monthday; D ++)
  67. {
  68. If(D <10) // format the output in the following four rows
  69. System. Out. Print (D + "");
  70. Else
  71. System. Out. Print (D + "");
  72. Weekday = (weekday + 1) % 7; // after each day is printed, the Second Day is the day of the week.
  73. If(Weekday = 0) // if the next day is Sunday, wrap the line.
  74. System. Out. println ();
  75. }
  76. System. Out. println ('/N ');
  77. }
  78. }

This program is seen in a book, but the middle method is not clear, the following section:

Code:
  1. Public static int firstday (INT y) // calculates the day of the week on the first day of the year.
  2. {
  3. Long n = y * 365;
  4. For (INT I = 1; I <Y; I ++)
  5. If (isleapyear (I ))
  6. N + = 1;
  7. Return (INT) n % 7;
  8. }

Thank you for your advice.

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.