Guest User

Untitled

a guest
Oct 25th, 2018
531
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 1.80 KB | None | 0 0
  1. package trinitySite;
  2.  
  3. import java.util.Scanner;
  4.  
  5. public class DayInYearV3 {
  6.    
  7.     int[][] a;
  8.    
  9.     public static void main(String[] args) {
  10.         Scanner sc = new Scanner(System.in);
  11.         DayInYearV3 diy = new DayInYearV3();       
  12.        
  13.         System.out.println("請輸入年月日(YYYY MM DD):");
  14.         int yy, mm, dd;
  15.         yy = sc.nextInt();
  16.         mm = sc.nextInt();
  17.         dd = sc.nextInt();
  18.         if(yy < 1)
  19.             yy = Math.abs(yy) - 1;                          //解決西元前問題
  20.        
  21.         diy.setEmptyCalendarOf(yy);
  22.        
  23.         if(diy.isInputLegal(yy, mm, dd)) {
  24.             diy.countDIY(yy, mm ,dd);
  25.             sc.close();
  26.         }
  27.     }
  28.    
  29.     public void setEmptyCalendarOf(int x) {
  30.         DayInYearV3 diy = new DayInYearV3();
  31.         a = new int[12][];                         
  32.        
  33.         for(int i = 0; i < 12; i++) {
  34.             if(i != 1 && i < 7 && i % 2 == 0) {            
  35.                 a[i] = new int[31];
  36.             } else if(i != 1 && i < 7 && i % 2 == 1) {
  37.                 a[i] = new int[30];
  38.             } else if(i >= 7 && i % 2 == 0) {
  39.                 a[i] = new int[30];
  40.             } else if(i >= 7 && i % 2 == 1) {
  41.                 a[i] = new int[31];
  42.             }
  43.         }
  44.        
  45.         a[1] = diy.isLeapYear(x) ? new int [29] : new int [28];
  46.     }
  47.    
  48.     public boolean isLeapYear(int year) {
  49.         if(year % 400 == 0)
  50.             return true;
  51.         else if(year % 4 == 0 && year % 100 != 0)
  52.             return true;
  53.         else
  54.             return false;
  55.     }
  56.    
  57.     public boolean isInputLegal(int yy, int mm, int dd) {
  58.         if(mm < 1 || mm > 12) {
  59.             System.out.println("月份錯誤");
  60.             return false;
  61.         }      
  62.         if(dd > a[mm - 1].length) {
  63.             System.out.println("日期錯誤");
  64.             return false;
  65.         } else
  66.             return true;
  67.     }
  68.    
  69.     public void countDIY(int yy, int mm, int dd) {
  70.         int count = 1;
  71.         for(int i = 0; i < mm; i++) {
  72.             for(int j = 0; j < a[i].length; j++) {
  73.                 if(i == mm - 1 && j == dd - 1)
  74.                     System.out.println("西元" + yy + "年,第" + count + "天");
  75.                 else {
  76.                 a[i][j] = count;
  77.                 count++;
  78.                 }
  79.             }
  80.         }
  81.     }
  82. }
Advertisement
Add Comment
Please, Sign In to add comment