java 获得非法的表达式开始错误

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/10767594/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-31 02:28:00  来源:igfitidea点击:

getting illegal start of expression error

java

提问by user1419170

i'm totally new to java. i 'm try to create my first program & i get this error.

我对java完全陌生。我正在尝试创建我的第一个程序 & 我收到此错误。

E:\java>javac Robot.java
Robot.java:16: error: illegal start of expression
public String CreateNew (); {
                       ^
Robot.java:16: error: ';' expected
public String CreateNew (); {
                       ^
2 errors

below is my program.

下面是我的程序。

public class Robot {
    public static void main(String args[]){
        String model;
        /*int year;*/
        String status;

        public String CreateNew () {
            Robot optimus;
            optimus = new Robot();
            optimus.model="Autobot"; 
            /*optimus.year="2008";*/
            optimus.status="active";
            return (optimus.model);
        }
    }
}

回答by T.J. Crowder

You're trying to define a method (CreateNew) withina method (main), which you cannot do in Java. Move it out of the main; and as modeland statusappear to be instance variables (not method variables), move them as well:

你试图定义一个方法(CreateNew的方法(main),你不能用Java做的。把它移出main; 并且modelstatus似乎是实例变量(不是方法变量),也移动它们:

public class Robot {
    // Member variables
    String model;
    /*int year;*/
    String status;

    // main method
    public static void main(String args[]){

        // Presumably more stuff here
    }

    // Further method    
    public String CreateNew () {
        Robot optimus;
        optimus = new Robot();
        optimus.model="Autobot"; 
        /*optimus.year="2008";*/
        optimus.status="active";
        return (optimus.model);
    }
}

Based on its content, you may want CreateNewto be static(so it can be called via Robot.CreateNewrather than via a Robotinstance). Like this:

根据其内容,您可能希望CreateNew成为static(因此可以通过Robot.CreateNew而不是通过Robot实例调用它)。像这样:

public class Robot {
    // Member variables
    String model;
    /*int year;*/
    String status;

    // main method
    public static void main(String args[]){

        // Presumably more stuff here
    }

    // Further method    
    public static String CreateNew () {
    //     ^----------------------------- here's the change
        Robot optimus;
        optimus = new Robot();
        optimus.model="Autobot"; 
        /*optimus.year="2008";*/
        optimus.status="active";
        return (optimus.model);
    }
}

Used as

用作

String theModel = Robot.CreateNew();

...although it's unclear to me why you want to create a Robotinstance and then throw it away and just return the modelinstance member's value.

...虽然我不清楚为什么要创建一个Robot实例然后将其丢弃并返回model实例成员的值。



Somewhat off-topic, but the overwhelming convention in Java is that method names (static or instance) start with a lower-case letter, e.g. createNewrather than CreateNew.

有点离题,但 Java 中的压倒性约定是方法名称(静态或实例)以小写字母开头,例如createNew而不是CreateNew.

回答by talnicolas

You didn't close your main method before you create the CreateNew()one. In fact I don't think you meant to have a main method in your Robot class, you should have only one main method for your whole program. And your CreateNew should be a constructor:

在创建 main 方法之前,您没有关闭CreateNew()它。事实上,我不认为你打算在你的 Robot 类中有一个 main 方法,你的整个程序应该只有一个 main 方法。你的 CreateNew 应该是一个构造函数

public class Robot {
        String model;
        /*int year;*/
        String status;

        public Robot () {
            this.model="Autobot"; 
            this.status="active";
        }
    }
}

and then in another class that contains your main method (or it could be in the same class too):

然后在另一个包含您的 main 方法的类中(或者它也可以在同一个类中):

public class OtherClass {
    public static void main(String[] args) {
        Robot optimus = new Robot(); // here you create an instance of your robot.
    }
}

then you can have a second constructor that takes in parameter the model and status like that:

那么你可以有第二个构造函数,它接受模型和状态的参数,如下所示:

public Robot (String m, Status s) {
        this.model=m; 
        this.status=s;
}

and finally in your main:

最后在您的主要内容中:

Robot prime = new Robot("aName", "aStatus");