java - "How to fix error ' 表达式的非法开始”- java

标签 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

最佳答案

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

添加括号也有帮助。

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

...而不是:

else if(year%4 !== 0)

关于java - "How to fix error ' 表达式的非法开始”- java,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/56208894/

相关文章:

ios - 指向接口(interface) id 的指针的算术,对于这个架构和平台来说,这不是一个恒定的大小

gwt - 使用跨站点支持编译 GWT 代码时出错

java - 这些Java错误是什么意思?

java - 如何使用pair返回jooq更新查询中的多列

java - 使用 java 流根据特定检查更新对象

java - JUnit 测试用例未正确执行

java - 何时抛出 IllegalStateException 与 IllegalArgumentException?

java - IllegalStateException:系统服务在 onCreate() 之前对 Activity 不可用

java - Android 非法状态异常 : The specified child already has a parent

java - UsbDevice requestPermission 被拒绝,且未显示请求对话框提示