java - 出现编译错误: “java:9: error: ' ;' expected” ;

标签 java if-statement compiler-errors

我收到此编译错误:

Solution.java:9: error: ';' expected
     boolean isEVen(){
                   ^

码:
import java.util.Scanner;

public class Solution {

    public static void main (String[] args){

        Scanner s = new Scanner(System.in);

        int n = s.nextInt();

        boolean isEVen(){
            return n%2==0;
        }


        if (isEVen() && n<=5 && n>=2){
            System.out.println("Not Weird");
        }else{
            if(!isEVen()){

                System.out.println("Weird");

            }else{
                if(isEVen() && n>=6 && n<=20){

                    System.out.println("Weird");

                } else{
                    if (isEVen() && n>20){

                        System.out.println("Not Weird");

                    } 
                }
            }

        }
    }    
}

我应该把分号放在哪里?

最佳答案

如下更新代码。

  • 将isEVen()方法移出main方法,并将int n作为参数传递给它。
  • import java.util.Scanner;
    
    public class Solution {
        public static void main(String[] args) {
    
            Scanner s = new Scanner(System.in);
            int n = s.nextInt();
    
            if (isEVen(n) && n <= 5 && n >= 2) {
                System.out.println("Not Weird");
            } else {
                if (!isEVen(n)) {
    
                    System.out.println("Weird");
    
                } else {
                    if (isEVen(n) && n >= 6 && n <= 20) {
    
                        System.out.println("Weird");
    
                    } else {
                        if (isEVen(n) && n > 20) {
    
                            System.out.println("Not Weird");
    
                        }
                    }
                }
    
            }
        }
    
        static boolean isEVen(int n) {
            return n % 2 == 0;
        }
    }
    

    关于java - 出现编译错误: “java:9: error: ' ;' expected” ;,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61382363/

    相关文章:

    java - 用于匹配用户输入中的字符串文字的正则表达式问题

    java - 是否可以对二维数组的第一列进行排序,但在java中保持相应的值相同

    language-agnostic - "if"语句是否被视为方法?

    Java服务器客户端网络

    java - 与 do while 循环和变量混淆

    if-statement - 为什么将最可能的条件放在 `if-else` 语句中?

    mysql删除和更新使用带有多个项目提交按钮的复选框

    visual-studio-2015 - CPLEX 12.8,Visual Studio 2015错误: Cannot open include file

    单独文件中的 C++ 类无法编译。已经在 Class.obj 中定义了一个或多个多重定义的符号

    build - AtmelStudio 7 默认代码不会生成 - 'no such file or directory' 错误