I am writing this code to determine whether a number is even or odd, if it's even it returns true, if it's not then it returns false. the given syntax is public boolean isEven(int n) so I can't change it.
class Main { // Given
public boolean isEven(int n) { // Given
if(n % 2==0){// I wrote from this
return true;
}else{
return false;
}
}
isEven(2); //To this
}
When I try to run this, I get these errors:
exit status 1
Main.java:14: error: invalid method declaration; return type required
isEven(2);
^
Main.java:14: error: illegal start of type
isEven(2);
^
2 errors
To invoke an instance method (one that's not static
), you need an instance on which to invoke:
class Main {
public boolean isEven(int n) {
return n % 2==0;
}
public static void main(String[] args) {
Main obj = new Main();
System.out.println(obj.isEven(2));
}
}
Note also the simplification of your code to a single statement.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.