weixin_38393017 2017-07-22 05:56 采纳率: 0%
浏览 912

中间那段代码是什么意思作用是什么?

import java.util.Scanner;

public class SmartDate {

private final int month;
private final int day;
private final int year;

public SmartDate(int m, int d, int y) {
    switch (m) {
    case 1:
    case 3:
    case 5:
    case 7:
    case 8:
    case 10:
    case 12:
        if (d > 0 && d <= 31) {
            month = m;
            day = d;
            year = y;
        } else {
            throw new IllegalArgumentException("Illegal date");
        }
        break;
    case 4:
    case 6:
    case 9:
    case 11:
        if (d > 0 && d <= 30) {
            month = m;
            day = d;
            year = y;
        } else {
            throw new IllegalArgumentException("Illegal date");
        }
        break;
    case 2:
        if (y % 4 == 0 && y % 100 != 0 || y % 400 == 0) {
            if (d > 0 && d <= 29) {
                month = m;
                day = d;
                year = y;
            } else {
                throw new IllegalArgumentException("Illegal date");
            }
        } else {
            if (d > 0 && d <= 28) {
                month = m;
                day = d;
                year = y;
            } else {
                throw new IllegalArgumentException("Illegal date");
            }
        }
        break;
    default:
        throw new IllegalArgumentException("Illegal date");
    }
}

public int month() {
    return month;
}

public int day() {
    return day;
}

public int year() {
    return year;
}

/**
 * Exercise 1.2.12
 * 
 * @return day of the week
 */
public String dayOfTheWeek() {
    // Zeller formula
    int month = this.month;
    int year = this.year;
    if (month <= 2) {
        month += 12;
        year--;
    }
    int week = (day + 2 * month + 3 * (month + 1) / 5 + year + year / 4 - year / 100 + year / 400) % 7;
    switch (week) {
    case 0:
        return "Monday";
    case 1:
        return "Tuesday";
    case 2:
        return "Wednesday";
    case 3:
        return "Thursday";
    case 4:
        return "Friday";
    case 5:
        return "Saturday";
    case 6:
        return "Sunday";
    default:
        return null;
    }
}


public String toString() {
    return month() + "/" + day() + "/" + year();
}

/*
public boolean equals(Object x) {
    if (this == x) {
        return true;
    }
    if (x == null) {
        return false;
    }
    if (this.getClass() != x.getClass()) {
        return false;
    }
    SmartDate that = (SmartDate) x;
    if (this.day != that.day) {
        return false;
    }
    if (this.month != that.month) {
        return false;
    }
    if (this.year != that.year) {
        return false;
    }
    return true;
}//这段代码的作用是什么?*/

public static void main(String[] args) {
    Scanner sc=new Scanner(System.in);
    int m = sc.nextInt();
    int d = sc.nextInt();
    int y = sc.nextInt();
    try {
        SmartDate date = new SmartDate(m, d, y);
        System.out.println(date);//??
        System.out.println(date.dayOfTheWeek());//??
    } catch (IllegalArgumentException e) {
        e.printStackTrace();
    }
}

}

  • 写回答

4条回答 默认 最新

  • JosBlue 2017-07-22 06:14
    关注

    不太清楚你具体问的是那一段代码,如果是dayOfTheWeek这个方法体中的,这里就是在通过一段自己设定的算法,通过获取到的天,月等时间计算出当天是星期几。具体的计算方式,就要思考一下了,
    你的问题描述的不够清晰

    评论

报告相同问题?

悬赏问题

  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题
  • ¥15 C#算法问题, 不知道怎么处理这个数据的转换
  • ¥15 YoloV5 第三方库的版本对照问题
  • ¥15 请完成下列相关问题!
  • ¥15 drone 推送镜像时候 purge: true 推送完毕后没有删除对应的镜像,手动拷贝到服务器执行结果正确在样才能让指令自动执行成功删除对应镜像,如何解决?
  • ¥15 求daily translation(DT)偏差订正方法的代码
  • ¥15 js调用html页面需要隐藏某个按钮