Java 方法名错误

Java method name error

当我尝试编译此代码时出现此错误:

dn09.java:38: error: illegal start of expression
                public Tip[] preberi (Scanner sc) {
                ^ dn09.java:38: error: ';' expected
                public Tip[] preberi (Scanner sc) {
                                    ^ dn09.java:38: error: ';' expected
                public Tip[] preberi (Scanner sc) {
                                                ^ 3 errors

[Napaka | process.javac]: Object reference not set to an instance of an object.

这是问题中的代码行:

public Tip[] preberi(Scanner sc) {
            Tip[] tipi = new tipi[d];
            for (int i = 0; i < tipi.length; i++) {
                String tip = sc.next();
                    switch (tip) {
                        case "prim":
                            tipi[i] = new Prim(sc.nextInt());
                            break;
                        case "arr":
                            tipi[i] = new Arr(sc.nextInt(), sc.nextInt());
                            break;
                        case "ostruct":
                            break;
                        case "pstruct":
                            break;
                    }
            }
            return tipi;
        }

我在 main() 方法中声明了我的 Scanner,它是导入的和所有内容。

正如你们中的一些人所问,这是我的全部代码(它根本不在工作状态,因为你们还会看到我是一个初学者所以它非常简单。

public class dn09 {
    public static void main(String[] args) {
    Scanner sc = new Scanner(System.in);
    int b = sc.nextInt();
    int d = sc.nextInt();
    Tip[] tipi = preberi(sc);
    int u = sc.nextInt();
    int[] ukazi = new int[u];
    for (int i = 0; i < u; i++) {
        ukazi[i] = sc.nextInt(); //if you know a better way to store 2 numbers where i could then
                                 //use the numbers separately that would be super helpfull as id        
    }                            //need it for 2 switch statements which im currenty trying to 
    for (int i = 0; i < u; i++) {//fit into 1.
        switch(ukazi[i]) {
            case 11:
                break;
            case 12:
                break;
            case 13:
                break;
            case 21:
                break;
            case 22:
                break;
            case 23:
                break;
            case 31:
                break;
            case 32:
                break;
            case 33:
                break;
        }
    }


    public Tip[] preberi(Scanner sc) {
        Tip[] tipi = new tipi[d];
        for (int i = 0; i < tipi.length; i++) {
            String tip = sc.next();
                switch (tip) {
                    case "prim":
                        tipi[i] = new Prim(sc.nextInt());
                        break;
                    case "arr":
                        tipi[i] = new Arr(sc.nextInt(), sc.nextInt());
                        break;
                    case "ostruct":
                        break;
                    case "pstruct":
                        break;
                }
        }
        return tipi;
    }
}

private static class Prim extends dn09 {
    protected int v;
    public static Prim (int v) {
        this.v = v;
    }
}

private static class Arr extends dn09 {
    protected int n;
    protected int t;
    public static Arr (int n, int t) {
        this.t = t;
        this.n = n;
    }
}

}

您的 main() 方法需要关闭 } 您只需关闭循环并切换。

您还需要从内部 类 构造函数(Prim(int)Arr(int,int))中删除两个 static。末尾有一个悬挂的 },也许你想删除关闭它的那个?

如果您使用 IDE 并自动缩进代码,这些问题很快就会显现出来。

public class dn09 {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        int b = sc.nextInt();
        int d = sc.nextInt();
        Tip[] tipi = preberi(sc);
        int u = sc.nextInt();
        int[] ukazi = new int[u];
        for (int i = 0; i < u; i++) {
            ukazi[i] = sc.nextInt(); //if you know a better way to store 2 numbers where i could then
            //use the numbers separately that would be super helpfull as id
        }                            //need it for 2 switch statements which im currenty trying to
        for (int i = 0; i < u; i++) {//fit into 1.
            switch(ukazi[i]) {
                case 11:
                    break;
...
            }
        }
    }

    public Tip[] preberi(Scanner sc) {
        Tip[] tipi = new tipi[d];
        for (int i = 0; i < tipi.length; i++) {
            String tip = sc.next();
            switch (tip) {
                case "prim":
                    liki[i] = new Prim(sc.nextInt());
                    break;
...
            }
        }
        return tipi;
    }

    private static class Prim extends dn09 {
        protected int v;
        public Prim (int v) {
            this.v = v;
        }
    }

    private static class Arr extends dn09 {
        protected int n;
        protected int t;
        public Arr (int n, int t) {
            this.t = t;
            this.n = n;
        }
    }
}