Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/loops/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Java 循环签入日期?_Java_Loops_Validation_Date - Fatal编程技术网

Java 循环签入日期?

Java 循环签入日期?,java,loops,validation,date,Java,Loops,Validation,Date,我有一个任务,用给定的方法和请求创建一个日期类。 其中一种方法以整数形式返回一周中的某一天,其中1=星期天…..6=星期五,0=星期六。 方法如下: public int dayInWeek () { int day, month; int year; //2 last numbers of the year int century; //2 first numbers of the year if(_month==1 || _month==2) {

我有一个任务,用给定的方法和请求创建一个日期类。 其中一种方法以整数形式返回一周中的某一天,其中1=星期天…..6=星期五,0=星期六。 方法如下:

public int dayInWeek () {
    int day, month;
    int year;    //2 last numbers of the year
    int century; //2 first numbers of the year
    if(_month==1 || _month==2) {
        day= _day;
        month= _month + 12;
        year= (_year-1) % 100;
        century= (_year-1) / 100;
    }
    else {
        day= _day;
        month= _month;
        year= _year % 100;
        century= _year / 100;
    }

    return (day + (26*(month+1))/10 + year + year/4 + century/4 - 2*century)%7;
} 
现在,在收到一周中的某一天之后,作业提示可能有一个日期的结果可能是否定的。 我试着制作一个
main()
,它将在给定的1800-2100年范围内的所有日子、月份和年份中循环,但只会越来越困惑和迷失。 如果有人能告诉我如何做到这一点,我将不胜感激,因为没有所有现有的日历/日期/等课程,这样对我来说太麻烦了。 谢谢

编辑:更多信息:

//constructors
/**
 * creates a new Date object if the date is valid, otherwise creates the date 1/1/2000
 * @param _day the day of the month (1-31)
 * @param _month the month in the year (1-12)
 * @param _year the year (1800-2100)
 */
public Date (int day, int month, int year) {
    if (isValidDate (day, month, year)) {
        _day=day;
        _month=month;
        _year=year;           
    }
    else {
        setToDefault();
    }
}

/**
 * copy constructor 
 * @param other the date to be copied
 */
public Date (Date other) {
    _day= other._day;
    _month= other._month;
    _year= other._year;
}
//methods
private boolean isValidDate(int day, int month, int year) {
    if (year<MIN_YEAR || year>MAX_YEAR) {
        return false;
    }
    if (month<MIN_MONTH || month>MAX_MONTH) {
        return false;
    }
    if (day<MIN_DAY) {
        return false;
    }
    switch (month) {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12: return day<=31;
        case 4:
        case 6:
        case 9:
        case 11: return day<=30;

        default: return leap(year) ? day<=29 : day<=28; //month==2;
    }        
}

/**
 * check if leap year
 * @param y year to check
 * @return true if given year is leap year
 */
private static boolean leap (int y) {
    return (y % 4 == 0 && y % 100 != 0) || (y % 400 == 0);
}
//构造函数
/**
*如果日期有效,则创建新的日期对象,否则将创建日期1/1/2000
*@param_day月日(1-31)
*@param_月一年中的月份(1-12)
*@param_年度(1800-2100)
*/
公开日期(整数天、整数月、整数年){
if(isValidDate(日、月、年)){
_天=天;
_月=月;
_年=年;
}
否则{
setToDefault();
}
}
/**
*复制构造函数
*@param other要复制的日期
*/
公开日期(其他日期){
_日=其他日;
_月份=其他月份;
_年份=其他。_年;
}
//方法
私有布尔值isValidDate(整数天、整数月、整数年){
如果(年最大年){
返回false;
}
如果(月最大月){
返回false;
}

如果(天当年份为2100时,您的结果为负值。使用今天的日期(月=3,日期=24,年份=2100)进行测试,您可以看到它给出了一个负数

如果您看一下,您可以通过将公式更改为:

return dayOfWeek= (day + (26*(month+1))/10 + year + year/4 + century/4+ 5*century)%7;

您能添加缺少的位和片段吗,即关于
\u month
,“\u year”的信息。这些值是如何定义的?@StefanFreitag嘿,刚刚添加了构造函数及其使用的方法。\u day/\u month/\u year是日期类的实例变量。从您的一般描述来看,很难理解您为什么会感到困惑。您能添加s吗一些日期与预期结果的示例以及观察到的结果如何不同?我相信这会有所帮助。@OleV.V.这就是问题所在。提示清楚地表明dayInWeek可能返回负数,我所有的手动检查都会产生正确的结果。这就是为什么我试图让程序在所有日期运行,如果dayInWeek结果为负数,打印该日期,这样我就可以看到问题的起因。但这也是我感到非常混乱的原因,我迷失了方向,并且对如何正确进行贯穿检查感到困惑。例如,d1.dayInWeek(),其中d1是日期为24.3.18的日期对象,将生成整数6(=星期五)。想解释一下为什么将-2改为+5会影响日计算,以及您是如何得出该公式的吗?当然,更新了答案,因此有了来源,请查看。希望有帮助!