当前位置:   article > 正文

用java write calendar

用java write calendar
  1. import java.util.Scanner;
  2. public class Test {
  3. public static void main(String[] args) {
  4. Scanner in = new Scanner(System.in);
  5. System.out.print("请输入年份:");
  6. int year = in.nextInt();
  7. System.out.print("请输入月份:");
  8. int month = in.nextInt();
  9. int days = 1;
  10. if (month == 2) {
  11. if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
  12. days = 29;
  13. } else {
  14. days = 28;
  15. }
  16. }
  17. if (month == 4 || month == 6 || month == 9 || month == 11) {
  18. days = 30;
  19. } else {
  20. days = 31;
  21. }
  22. int sum = 0;
  23. for (int i = 1900; i < year; i++) {
  24. if (i % 4 == 0 && i % 100 != 0 || i % 400 == 0) {
  25. sum += 366;
  26. } else {
  27. sum += 365;
  28. }
  29. }
  30. for (int i = 1; i < month; i++) {
  31. if (i == 2) {
  32. if (year % 4 == 0 && year % 100 != 0 || year % 400 == 0) {
  33. sum += 29;
  34. } else {
  35. sum += 28;
  36. }
  37. } else {
  38. if (i == 4 || i == 6 || i == 9 || i == 11) {
  39. sum += 30;
  40. } else {
  41. sum += 31;
  42. }
  43. }
  44. }
  45. sum += 1;
  46. int wekday = sum % 7;
  47. System.out.println("日\t一\t二\t三\t四\t五\t六");
  48. for (int i = 1; i <= wekday; i++) {
  49. System.out.print("\t");
  50. }
  51. for (int i = 1; i <= days; i++) {
  52. if (sum % 7 == 6) {
  53. System.out.print(i + "\n");
  54. } else {
  55. System.out.print(i + "\t");
  56. }
  57. sum++;
  58. }
  59. }
  60. }

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/寸_铁/article/detail/750716
推荐阅读
相关标签
  

闽ICP备14008679号