繁体   English   中英

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

[英]“How to fix error 'Illegal start of expression” - java

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

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

错误:

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

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

添加括号也有帮助。

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

…代替:

else if(year%4 !== 0)

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM