如何正确使用switch语句?

时间:2016-10-14 17:07:56

标签: 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的子类。

我开始创建它,但问题是;我不能告诉<或者>在转换语句中。我该怎么办?这是我的课程:

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;
    }
}

...

public class NextDay {

    public static void main(String args[]) throws Exception {
        Date dateObj = new Date(20, 5, 2016);
        System.out.println("Old Date: " + dateObj.getDay() + "." + dateObj.getMonth() + "." + dateObj.getYear() + ".");
        System.out.println("The next day is " + dateObj.getNextDay().toString() + ".");
    }
}

2 个答案:

答案 0 :(得分:2)

你说你想要switch内的“或”声明,对吧?如果您逐行编写case标签,则会收到“或声明”,如下所示:

switch(variable){
        case 1:
        case 2:
        case 3: {
            //.. when variable equals to 1 or 2 or 3
        }
    }

这样,您可以像这样编写getMaxDaysInMonth方法:

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++;
    }    
}

答案 1 :(得分:1)

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

如果您必须使用开关案例,您应该使用它来设置当月的最大日期,然后检查,如果您当前的日期超过这个最大日期:

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();
}