gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-02 09:52:41 25 4
gpt4 key购买 nike

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

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

错误:

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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com