Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package trinitySite;
- import java.util.Scanner;
- public class DayInYear {
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- int[][] a = new int[12][]; //產生空月曆
- for(int i = 0; i < 12; i++) {
- if(i != 1 && i < 7 && i % 2 == 0) { //填入所有月份(沒有2月)
- a[i] = new int[31];
- } else if(i != 1 && i < 7 && i % 2 == 1) {
- a[i] = new int[30];
- } else if(i >= 7 && i % 2 == 0) {
- a[i] = new int[30];
- } else if(i >= 7 && i % 2 == 1) {
- a[i] = new int[31];
- }
- }
- System.out.println("請輸入年月日(YYYY MM DD):");
- int yy, mm, dd;
- yy = sc.nextInt();
- mm = sc.nextInt();
- dd = sc.nextInt();
- if(yy < 1)
- yy = Math.abs(yy) - 1; //解決西元前問題
- boolean isLeapYear;
- if(yy % 400 == 0)
- isLeapYear = true;
- else if(yy % 4 == 0 && yy % 100 != 0)
- isLeapYear = true;
- else
- isLeapYear = false;
- a[1] = isLeapYear ? new int [29]: new int [28]; //計算是否閏年
- if(mm < 1 || mm > 12) {
- System.out.println("月份錯誤");
- sc.close();
- return;
- }
- if(dd > a[mm - 1].length) {
- System.out.println("日期錯誤");
- } else {
- int count = 1;
- for(int i = 0; i < mm; i++) {
- for(int j = 0; j < a[i].length; j++) {
- if(i == mm - 1 && j == dd - 1)
- System.out.println("西元" + yy + "年,第" + count + "天");
- else {
- a[i][j] = count;
- count++;
- }
- }
- }
- }
- sc.close();
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement