• Java50道经典习题-程序14 求日期


    题目:输入某年某月某日,判断这一天是这一年的第几天?
    分析:(1)以3月5日为例,应该先把前两个月的加起来,然后再加上5天即本年的第几天
    (2)特殊情况,闰年2月份的天数是29天,否则是28天

     1 import java.util.Scanner;
     2 public class Prog14 {
     3     public static void main(String[] args) {
     4         Scanner scan1=new Scanner(System.in);
     5         System.out.println("请输入年份:");
     6         int year=scan1.nextInt();
     7         Scanner scan2=new Scanner(System.in);
     8         System.out.println("请输入月份:");
     9         int month=scan2.nextInt();
    10         Scanner scan3=new Scanner(System.in);
    11         System.out.println("请输入几号:");
    12         int date=scan3.nextInt();
    13         scan1.close();
    14         scan2.close();
    15         scan3.close();
    16         System.out.println(year+"年"+month+"月"+date+"日"+"是这一年的第"+dijitian(year,month,date)+"天");
    17     }
    18     //求解天数
    19     private static int dijitian(int year,int month,int date) {
    20         int n=0;
    21         int[] month_date=new int[] {0,31,28,31,30,31,30,31,31,30,31,30};
    22         if((year%400)==0||((year%4)==0)&&((year%100)!=0))//判断闰年
    23             month_date[2]=29;
    24         for(int i=0;i<month;i++)
    25             n+=month_date[i];
    26         return n+date;
    27     }
    28 }
    29 /*运行结果
    30 请输入年份:
    31 2018
    32 请输入月份:
    33 9
    34 请输入几号:
    35 4
    36 2018年9月4日是这一年的第247天
    37 */
  • 相关阅读:
    Sprinig.net 双向绑定 Bidirectional data binding and data model management 和 UpdatePanel
    Memcached是什么
    Spring.net 网络示例 codeproject
    jquery.modalbox.show 插件
    UVA 639 Don't Get Rooked
    UVA 539 The Settlers of Catan
    UVA 301 Transportation
    UVA 331 Mapping the Swaps
    UVA 216 Getting in Line
    UVA 10344 23 out of 5
  • 原文地址:https://www.cnblogs.com/parkour1026/p/10796826.html
Copyright © 2020-2023  润新知