簡體   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