- import java.util.Scanner;
- public class HowManyDay {
- public static Scanner sc;
- private static int year;
- private static int month;
- private static int day;
- private static boolean b;
- private static boolean b1;
- private static boolean b2;
- public static void main(String[] args) {
-
- chooseyear();
- }
- //选择年份并判断,将年份控制在1000-3000之间。
- public static void chooseyear() {
- sc = new Scanner(System.in);
- System.out.println("请输入年份:");
-
- year = sc.nextInt();
- while(year<1000|year>3000) {
- System.out.println("输入的年份不合法,请在1000-3000之间选择年份:");
- year = sc.nextInt();
- }
- choosemonth(year);
- }
- //选择月份
- public static void choosemonth(int year) {
- sc = new Scanner(System.in);
-
- System.out.println("请输入月份:");
- month = sc.nextInt();
- while (month>12 | month<1){
- System.out.println("输入的月份不合法,重新输入:");
- month = sc.nextInt();
- }
-
- chooseday(year,month);
- }
-
-
- //选择月的号数
- public static void chooseday(int year,int month) {
- sc = new Scanner(System.in);
- //判断是否为闰年
- b = isLeapYear(year);
- //1 3 5 7 8 10 12 这几个月每个月有31天
- int[] arr1 ={1,3,5,7,8,10,12};
- //查看输入的月份是否在数组之中
- b1= whitchMonth(arr1,month);
- // 4 6 9 11 每个月有 30天
- int[] arr2 = {4,6,9,11};
-
- b2= whitchMonth(arr2,month);
- System.out.println("请输入日数:");
-
- day = sc.nextInt();
-
- while(day<=0){
- System.out.println("天数为正整数重新输入:");
- day = sc.nextInt();
- }
- //非闰年的情况
- if(b==false) {
- //处理输入的天数是否正确
- // 2月的情况
- if(b1==false && b2==false){
- while(day>28){
- System.out.println(year+"年"+month+"月最多只有28天,请重新输入:");
- day = sc.nextInt();
- }
- getResult(year,month,day,b);
-
- }
- //非2月的情况
- isDayTrue(b1,year,month,day,b);
- }
-
- //闰年时候的判断
- if(b==true) {
- //2月的时候
- if(b1==false && b2==false){
- while(day>29){
- System.out.println(year+"年"+month+"月最多只有29天,请重新输入:");
- day = sc.nextInt();
- }
-
- getResult(year,month,day,b);
- }
-
- //非2月的时候
- isDayTrue(b1,year,month,day,b);
- }
-
- }
- //判断输入的月数在哪个数组的方法。
- private static boolean whitchMonth(int[] arr1, int month2) {
-
- for (int i = 0; i < arr1.length; i++) {
- if(arr1[i]!=month2)
- continue;
- return true;
- }
-
- return false;
- }
-
- //当不是二月的时候对天数的处理
- public static void isDayTrue(boolean b1, int year, int month,
- int day, boolean b) {
-
- if(b1==true && b2==false) {
-
- while(day>31){
- System.out.println(month+"月最多只有31天,请重新输入");
- day = sc.nextInt();
- }
-
- getResult(year,month,day, b);
-
- }
-
- else if(b2==true &&b1==false){
-
- while(day>30){
- System.out.println(month+"月最多只有30天,请重新输入");
- day = sc.nextInt();
- }
-
- getResult(year,month,day,b);
- }
- }
-
-
- //计算天数的方法。
- private static void getResult(int year,int month, int day,boolean b) {
- int result = 0;
- //将每个月的天数变成数组。在数组前加上0元素时为了让月数和数组的角标值匹配
- int[] arr = {0,31,28,31,30,31,30,31,31,30,31,30,31};
- //非闰年的情况下第二个月为28天
- if(b==false){
- arr[2]=28;
- }
- //闰年时候2月为29天
- else{
- arr[2]=29;
- }
- for (int i = 1; i < month; i++) {
- result+=arr[i];
- }
-
- System.out.println(year+"年"+month+"月"+day+"日"+"是该年的第"+(result+day)+"天");
- }
-
- //判断是否为闰年
- public static boolean isLeapYear(int year) {
-
- if (year%4==0) {
- if (year%100==0) {
- if(year%400==0)
- return true;
- return false;
- }
- return true;
- }
- return false;
- }
- }
复制代码 |