简体   繁体   English

编译错误:预期为标识符

[英]compilation error: identifier expected

import java.io.*;

public class details
{
    BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
    System.out.println("What is your name?");
    String name = in.readLine(); ;
    System.out.println("Hello " + name);
}

my problem with this code is that i get " identifier expected " when compiling.. can anyone help me on what to do? 我的这段代码的问题是,我在编译时会收到“期望的标识符” ..有人可以帮我做什么吗? or better, can anyone provide the code that would use BufferedReader to prompt the name, address, and age then finally would display the output.. 或更好的是,谁能提供使用BufferedReader提示名称,地址和年龄的代码,然后最终将显示输出。

thanks!! 谢谢!!

You have not defined a method around your code. 您尚未在代码周围定义方法。

import java.io.*;

public class details
{
    public static void main( String[] args )
    {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("What is your name?");
        String name = in.readLine(); ;
        System.out.println("Hello " + name);
    }
}

In this case, I have assumed that you want your code to be executed in the main method of the class. 在这种情况下,我假设您希望在类的main方法中执行您的代码。 It is, of course, possible that this code goes in any other method. 当然,此代码也可以采用其他任何方法。

You must to wrap your following code into a block (Either method or static). 必须将以下代码包装为一个块(方法或静态)。

BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
System.out.println("What is your name?");
String name = in.readLine(); ;
System.out.println("Hello " + name);

Without a block you can only declare variables and more than that assign them a value in single statement. 如果没有块,则只能声明变量,并且不能在单个语句中为其赋值。

For method main() will be best choice for now: 对于方法main()来说,现在是最佳选择:

public class details {
    public static void main(String[] args){
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("What is your name?");
        String name = in.readLine(); ;
        System.out.println("Hello " + name);
    }
}

or If you want to use static block then... 或如果您想使用静态块,则...

public class details {
    static {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("What is your name?");
        String name = in.readLine(); ;
        System.out.println("Hello " + name);
    }
}

or if you want to build another method then.. 或者如果您想构建另一种方法。

public class details {
    public static void main(String[] args){
        myMethod();
    }
    private static void myMethod(){
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("What is your name?");
        String name = in.readLine(); ;
        System.out.println("Hello " + name);
    }
}

Also worry about exception due to BufferedReader . 还担心由于BufferedReader而导致的异常。

You also will have to catch or throw the IOException. 您还必须捕获或抛出IOException。 See below. 见下文。 Not always the best way, but it will get you a result: 并非总是最好的方法,但是它将为您带来结果:

public class details {
    public static void main( String[] args) throws IOException {
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("What is your name?");
        String name = in.readLine(); ;
        System.out.println("Hello " + name);
    }
}

only variable/object declaration statement are written outside of method 仅变量/对象声明语句写在方法之外

public class details{
    public static void main(String arg[]){
        BufferedReader in = new BufferedReader(new InputStreamReader(System.in));
        System.out.println("What is your name?");
        String name = in.readLine(); ;
        System.out.println("Hello " + name);
    }
}

here is example try to learn java book and see the syntax then try to develop the program 这是示例,尝试学习Java书籍并查看语法,然后尝试开发程序

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM