【问题标题】:Java method name error [closed]Java方法名称错误[关闭]
【发布时间】:2015-01-10 16:32:02
【问题描述】:

当我尝试编译这段代码时,我得到了这个错误:

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 方法中是否也声明了此方法?
  • 粘贴整个 dn09 类代码。
  • 问题似乎出在您发布的代码之前。发布一个可运行的最小示例。此外,通常您应该使用像 Eclipse 这样的优秀 IDE 来帮助解决编译时错误。
  • 您要切换数组 (switch (tipi))?这行不通。
  • 因为 public Tip[] preberi(Scanner sc) 方法是 inside main 方法。那是行不通的。

标签: java methods naming


【解决方案1】:

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

您还需要从内部类构造函数中删除两个staticPrim(int)Arr(int,int))。最后有一个悬空的},也许您想删除关闭它的那个?

如果您使用 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;
        }
    }
}

【讨论】:

  • 额外的 } 在那里是因为我在努力发布代码(刚开始使用该网站),即使在从所有构造函数中删除静态之后我仍然收到相同的错误
  • @LukaTheLegend 您需要阅读我所有的答案(尤其是关于关闭 main 方法)。我为您重新格式化并添加了大纲。顺便说一句:您需要 Java7 才能打开字符串。
  • 我现在收到一组错误,告诉我找不到提示
  • 您没有提供导入,因此我们无法知道您正在使用哪些类。你不应该使用你没有的类...
  • 我没有使用任何其他类,当我将方法放在主方法之外时,它现在将该方法视为一个类
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-12-10
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多