美文网首页基础练习题
14 输入某年某月某日,判断这一天是这一年的第几天

14 输入某年某月某日,判断这一天是这一年的第几天

作者: 北极的大企鹅 | 来源:发表于2023-10-05 23:15 被阅读0次

    题目:

    输入某年某月某日,判断这一天是这一年的第几天? 1. 程序分析:以 3 月 5 日为例,应该先把前两个月的加起来, 然后再加上 5
    天即本年的第几天, 特殊情况,闰年且输入月份大于 3 时需考虑多加一天。

    
     1     public class _014JudgmentDate {
     2 
     3     public static void main(String[] args) {
     4         while (true) {
     5             days();
     6         }
     7     }
     8 
     9     private static void days() {
    10         int year, month, day;
    11         int days = 0;
    12         int feb = 28;
    13 
    14         Scanner scanner = new Scanner(System.in);
    15         System.out.println("请输入年份:");
    16         year = scanner.nextInt();
    17         System.out.println("请输入月份:");
    18         month = scanner.nextInt();
    19         System.out.println("请输入天数");
    20         day = scanner.nextInt();
    21 
    22         if (year < 0 || month < 0 || month > 12 || day < 0 || day > 31) {
    23             System.out.println("请重新输入");
    24         } else {
    25 
    26             for (int i = 1; i < month; i++) {
    27                 switch (month) {
    28                 case 1:
    29                     days = day;
    30                     break;
    31                 case 2:
    32                     if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0) {
    33                         feb = 29;
    34                     } else {
    35                         feb = 28;
    36                     }
    37                     days = 31 + day;
    38                     break;
    39                 case 3:
    40                     days = 31 + feb + day;
    41                     break;
    42                 case 4:
    43                     days = 31 + feb + 31 + day;
    44                     break;
    45                 case 5:
    46                     days = 31 + feb + 31 + 30 + day;
    47                     break;
    48                 case 6:
    49                     days = 31 + feb + 31 + 30 + 31 + day;
    50                     break;
    51                 case 7:
    52                     days = 31 + feb + 31 + 30 + 31 + 30 + day;
    53                     break;
    54                 case 8:
    55                     days = 31 + feb + 31 + 30 + 31 + 30 + 31 + day;
    56                     break;
    57                 case 9:
    58                     days = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + day;
    59                     break;
    60                 case 10:
    61                     days = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + day;
    62                     break;
    63                 case 11:
    64                     days = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
    65                             + day;
    66                     break;
    67                 case 12:
    68                     days = 31 + feb + 31 + 30 + 31 + 30 + 31 + 31 + 30 + 31
    69                             + 30 + day;
    70                 }
    71                 System.out.println(year + " 年 " + month + " 月 " + day
    72                         + "号是今年的第" + days + "天");
    73             }
    74         }
    75     }
    76 
    77 }
     
    

    相关文章

      网友评论

        本文标题:14 输入某年某月某日,判断这一天是这一年的第几天

        本文链接:https://www.haomeiwen.com/subject/sgusbdtx.html