首页 文章

非法开始表达(方法)[关闭]

提问于
浏览
-1

决定最近学习一点java,我被困在第一关 . 这是我非常基本的代码:

import java.util.Scanner;

class helloWorld{

public static void main(String[] args){
    Scanner user_input = new Scanner(System.in);

    int a = 50;
    String first_name;
    String last_name;

    public static int funcName(int a, int b) {
    }
}
}

据我所知,没有错误 . 但是,在编译时我收到此错误:

Dominics-MacBook-Pro:helloworld dominicsore$ javac helloworld.java
helloworld.java:12: error: illegal start of expression
public static int funcName(int a, int b) {
^
helloworld.java:12: error: illegal start of expression
public static int funcName(int a, int b) {
       ^
helloworld.java:12: error: ';' expected
public static int funcName(int a, int b) {
             ^
helloworld.java:12: error: '.class' expected
public static int funcName(int a, int b) {
                               ^
helloworld.java:12: error: ';' expected
public static int funcName(int a, int b) {
                                ^
helloworld.java:12: error: ';' expected
public static int funcName(int a, int b) {
                                       ^
6 errors

我搜索和搜索过,所有常见的回答都是拼写错误和错位的括号,但据我所知,情况并非如此 .

不确定它是否有任何区别,但我在Mac上,使用vim编辑器,我正在从终端编译 .

任何建议表示赞赏 .

2 回答

  • 3

    您不能在另一个方法中声明方法 . 在 main 方法之外移动 funcName

    import java.util.Scanner;
    
    class helloWorld{
    
        public static void main(String[] args){
            Scanner user_input = new Scanner(System.in);
            int a = 50;
            String first_name;
            String last_name;
            //do something more here, probably to call
            //to your funcName method
        }
    
        public static int funcName(int a, int b) {
            //method implementation
            //since it doesn't return anything (yet), I add this line
            //just for compilation purposes
            return 0;
        }
    }
    
  • 1

    funcName 正在主方法中定义,它必须在它之外:

    import java.util.Scanner;
    
    class helloWorld{
    
        public static void main(String[] args){
            Scanner user_input = new Scanner(System.in);
    
            int a = 50;
            String first_name;
            String last_name;
        }
        public static int funcName(int a, int b) {
    
        }
    }
    

相关问题