gpt4 book ai didi

java - 出现无法解决的错误

转载 作者:行者123 更新时间:2023-12-01 14:37:40 25 4
gpt4 key购买 nike

编译我的java程序后,我遇到了一堆错误,但我不知道如何解决它们。有人可以帮我吗? =/

class Date{

public static boolean isLeapYear(int year){
boolean answer = false;

if(year % 4 == 0 &&(year % 400 == 0 || year % 100 != 0))
answer = true;
else
answer = false;

return answer;
}
}


int returnDaysInMonth (int year, int month){

if (month==1||month==3||month==5||month==7||month==8||month==10||month==12){

return 31;

} else if (month==4||month==6||month==9||month==11){

return 30;

} else if(isLeapYear(year)){

return 29;
} else {
return 28;

}
}

以下是编译错误:

/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:22: class, interface, or enum expected
public int returnDaysInMonth (int year, int month){
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:28: class,
interface, or enum expected
} else if (month==4||month==6||month==9||month==11){
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:32: class,
interface, or enum expected
} else if(isLeapYear(year)){
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:35: class,
interface, or enum expected
} else {
/Users/vlop/NetBeansProjects2/JavaLibrary1/src/Date.java:38: class,
interface, or enum expected
}
5 errors

最佳答案

在类声明关闭后,您正尝试执行大量 Java 操作。

正确的缩进有助于识别此类问题:

class Date {
public static boolean isLeapYear(int year) {
boolean answer = false;

if(year % 4 == 0 &&(year % 400 == 0 || year % 100 != 0))
answer = true;
else
answer = false;

return answer;
}
} // End of class.

// WAT
int returnDaysInMonth (int year, int month) {

// etc.

另外,单独的问题,但您可以直接返回 boolean 表达式的结果:

public static boolean isLeapYear(int year) {
return ((year % 4 == 0) && ((year % 400 == 0) || (year % 100 != 0)));
}

即使您对此感到不舒服,您仍然可以避免一些噪音:

public static boolean isLeapYear(int year) {
if (year % 4 == 0 &&(year % 400 == 0 || year % 100 != 0)) {
return true;
}
return false;
}

关于java - 出现无法解决的错误,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16328955/

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