Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package trinitySite;
- import java.util.Scanner;
- public class DayInYearV3 {
- int[][] a;
- public static void main(String[] args) {
- Scanner sc = new Scanner(System.in);
- DayInYearV3 diy = new DayInYearV3();
- 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; //解決西元前問題
- diy.setEmptyCalendarOf(yy);
- if(diy.isInputLegal(yy, mm, dd)) {
- diy.countDIY(yy, mm ,dd);
- sc.close();
- }
- }
- public void setEmptyCalendarOf(int x) {
- DayInYearV3 diy = new DayInYearV3();
- a = new int[12][];
- for(int i = 0; i < 12; i++) {
- if(i != 1 && i < 7 && i % 2 == 0) {
- 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];
- }
- }
- a[1] = diy.isLeapYear(x) ? new int [29] : new int [28];
- }
- public boolean isLeapYear(int year) {
- if(year % 400 == 0)
- return true;
- else if(year % 4 == 0 && year % 100 != 0)
- return true;
- else
- return false;
- }
- public boolean isInputLegal(int yy, int mm, int dd) {
- if(mm < 1 || mm > 12) {
- System.out.println("月份錯誤");
- return false;
- }
- if(dd > a[mm - 1].length) {
- System.out.println("日期錯誤");
- return false;
- } else
- return true;
- }
- public void countDIY(int yy, int mm, int dd) {
- 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++;
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment