“如何修复错误'表达式的非法开头”-Java

时间:2019-05-19 14:43:55

标签: java compiler-errors illegalstateexception

我基本上是在完善,完成并尝试编译我的代码以进行练习。目的是创建日历。

无法找出问题,已尝试查找不合适的语句,但没有解决方案。我试图找到其他来源以获得解决方案,但我无法找出问题所在。

错误:

MyCalendar.java:60: illegal start of expression
            else if(year%4!==0)
                            ^

代码:

public class MyCalender {

    int day, month, year;

    boolean isDateValid = true;

    public MyCalender(int day, int month, int year) {

        this.day = day;
        this.month = month;
        this.year = year;

        if (month > 12)   //Day and Month validation
        {
            isDateValid = false;
        } else if (month == 1 || month == 3 || month == 5 || month == 7 || month == 9 || month == 12) {

            if (day <= 31) {
                isDateValid = true;
            } else if (day >= 31) {
                isDateValid = false;
            }
        } else if (month == 2 || month == 4 || month == 6 || month == 8 || month = 10 || month == 12) {

            if (day <= 30) {
                isDateValid = true;
            } else if (day >= 30) {

                isDateValid = false;
            }
        } else if (month == 2) //Consideration of February month and leap year validation
        {
            if (year % 4 == 0) {
                if (day <= 29) {
                    isDateValid = true;
                } else if (day >= 29) {
                    isDateValid = false;
                }
            } else if (year % 4 != = 0) {
                if (day <= 28) {
                    isDateValid = true;
                } else if (day >= 28) {
                    isDateValid = false;
                }
            }
        }
    }

    boolean isDateValid() {
        if (isDateValid) {
            System.out.println("is a Valid Date");

            return true;
        }
        if (!isDateValid) {
            System.out.println("is not a Valid Date,please re-input Date");

            return false;
        }
        return isDateValid;
    }

    public int getDay() {
        return day;
    }

    public int getMonth() {
        return month;
    }

    public int getYear() {
        return year;
    }

    public static void main(String[] args) {

        MyCalender d = new MyCalender(29, 02, 2019);
        System.out.println("Date" + d.getDay() + "/" + d.getMonth() + "/" + d.getYear());
        d.isDateValid();

        MyCalender d1 = new MyCalender(25, 02, 2019);
        System.out.println("Date" + d1.getDay() + "/" + d1.getMonth() + "/" + d1.getYear());
        d1.isDateValid();
    }
}

预期输出为:

java MyCalendar 29/02/2019
29/02/2019 in not a valid date, please re-input a valid date: 25/05/2019
25/05/2019 is a Saturday and located in the fourth week of May 2019
The calendar of May 2019 is:
SUN MON TUE WED THU FRI SAT
1 2 3 4
5 6 7 8 9 10 11
12 13 14 15 16 17 18
19 20 21 22 23 24 25
26 27 28 29 30 31

1 个答案:

答案 0 :(得分:1)

您必须使用!=而不是!==

添加括号也有帮助。

else if( (year%4) != 0 )

…代替:

else if(year%4 !== 0)