Advertisement
Guest User

Untitled

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