Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/java/356.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 如何正确使用switch语句?_Java_Switch Statement - Fatal编程技术网

Java 如何正确使用switch语句?

Java 如何正确使用switch语句?,java,switch-statement,Java,Switch Statement,我需要编写一个Java程序(类NextDay),通过输入日期、月份和年份来计算和打印第二天的日期 示例调用和输出: Actual Date 12.12.2004 The next day is the 13.12.2004. 还应检查输入的正确性!如果输入了错误的日期组合(例如32 12 2007),将抛出异常InvalidDateArgumentsException。以合适的形式将此类定义为类java.lang.Exception的子类 我开始创造它,但问题是;我不能在switch语句中说出

我需要编写一个Java程序(类NextDay),通过输入日期、月份和年份来计算和打印第二天的日期

示例调用和输出:

Actual Date 12.12.2004
The next day is the 13.12.2004.
还应检查输入的正确性!如果输入了错误的日期组合(例如32 12 2007),将抛出异常
InvalidDateArgumentsException
。以合适的形式将此类定义为类
java.lang.Exception
的子类

我开始创造它,但问题是;我不能在switch语句中说出。我该怎么办?以下是我的课程:

public class Date {

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

    public Date(int day, int month, int year) {
        this.day = day;
        this.month = month;
        this.year = year;
    }

    public Date getNextDay() throws Exception {
        if (isLeapYear() == true) {
            switch (month) {

                case 1:
                    day = 31;
                    break;

                case 2:
                    day = 29;
                    break;

                case 3:
                    day = 31;
                    break;

                case 4:
                    day = 30;
                    break;

                case 5:
                    day = 31;
                    break;

                case 6:
                    day = 30;
                    break;

                case 7:
                    day = 31;
                    break;

                case 8:
                    day = 31;
                    break;

                case 9:
                    day = 30;
                    break;

                case 10:
                    day = 31;
                    break;

                case 11:
                    day = 30;
                    break;

                case 12:
                    day = 31;
                    break;
            }

        }
        return new Date(day + 1, month, year);

    }

    public int getDay() {
        return day;
    }

    public int getMonth() {
        return month;
    }

    public int getYear() {
        return year;
    }

    public boolean isLeapYear() {
        if (year % 4 == 0 && year % 100 != 0 && year % 400 == 0) {
            return true;
        }
        return false;
    }

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


编辑,因为我们不能只使用标准类

如果必须使用switch case,则应使用它设置当月的最大天数,然后检查当前天数是否超过此最大天数:

int maxDay;
switch (month) {
case 1: maxDay = 31; break;
case 2: maxDay = isLeapYear() ? 29 : 28; break;
case 3: maxDay = 31; break;
// ... other months ...
case 12: maxDay = 31; break;
default: throw new InvalidDateArgumentsException();
}
if (isLeapYear()) {
    maxDay = 29;
}
if (day > maxDay) {
    throw new InvalidDateArgumentsException();
}

您的意思是希望在
开关中包含一个“or”语句,对吗?如果你一行一行地写
case
标签,你会得到“or statement”,如下所示:

switch(variable){
        case 1:
        case 2:
        case 3: {
            //.. when variable equals to 1 or 2 or 3
        }
    }
int getMaxDaysInMonth()
{
    int daysInMonth = 0;
    switch(month)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            daysInMonth = 31;
            break;
        case 2:
            if(isLeapYear())
            {
                daysInMonth = 29;
            }
            else
            {
                daysInMonth = 28;
            }
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            daysInMonth = 30;   
    }

    return daysInMonth;
}
因此,您可以这样编写
getMaxDaysInMonth
方法:

switch(variable){
        case 1:
        case 2:
        case 3: {
            //.. when variable equals to 1 or 2 or 3
        }
    }
int getMaxDaysInMonth()
{
    int daysInMonth = 0;
    switch(month)
    {
        case 1:
        case 3:
        case 5:
        case 7:
        case 8:
        case 10:
        case 12:
            daysInMonth = 31;
            break;
        case 2:
            if(isLeapYear())
            {
                daysInMonth = 29;
            }
            else
            {
                daysInMonth = 28;
            }
            break;
        case 4:
        case 6:
        case 9:
        case 11:
            daysInMonth = 30;   
    }

    return daysInMonth;
}
此外,您正在错误地检查闰年。以下是正确的方法:

boolean isLeapYear(){
        return (year % 4 == 0 && year % 100 != 0) || (year % 400 == 0);
    }
以下是您每天的增量:

void incrementDate(){
    if((day + 1) > getMaxDaysInMonth())
    {
        day = 1;        
        if (month == 12)
        {
            month = 1;
            year++;
        }else{   
          month++;
    }
    } else {
       day++;
    }    
}

我会的,但他们告诉我应该使用switch case,这是因为我的大学,我是初学者,我们正在学习switch case。你必须定义自己的
日期
类吗
java.util.Date
存在,你知道不,我不必,我想:)对不起,我耽误了你的时间,我的英语不太好,这就是我没有正确理解问题的原因。我们必须创建一个日期类。非常感谢:)