java - 错误:<标识符> 预期

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20182723/
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-08-12 23:48:08  来源:igfitidea点击:

java - error: <identifier> expected

java

提问by VVMC23

I am trying to learn java with this website: http://www.homeandlearn.co.uk/java/user_input.htmlBut when I run the code in Ubuntu through the terminal I get these errors:

我正在尝试通过以下网站学习 Java:http: //www.homeandlearn.co.uk/java/user_input.html但是当我通过终端在 Ubuntu 中运行代码时,出现以下错误:

vonvic@BSW-Computer:~/Java_Applications$ javac StringVariables2.java
StringVariables2.java:3: error: '{' expected
    public static class main(String[] args) {
                        ^
StringVariables2.java:8: error: <identifier> expected
        System.out.println("Please enter your first name")
                      ^
StringVariables2.java:8: error: illegal start of type
        System.out.println("Please enter your first name")
                           ^
StringVariables2.java:8: error: ';' expected
    System.out.println("Please enter your first name")
                                                      ^
StringVariables2.java:12: error: <identifier> expected
    System.out.println("Please enter your family name")
                      ^
StringVariables2.java:12: error: illegal start of type
    System.out.println("Please enter your family name")
                       ^
StringVariables2.java:12: error: ';' expected
    System.out.println("Please enter your family name")
                                                       ^
StringVariables2.java:16: error: <identifier> expected
    full_name = first_name +" "+ family_name;
             ^
StringVariables2.java:18: error: <identifier> expected
    System.out.println("You are" + full_name);
                      ^
StringVariables2.java:18: error: illegal start of type
    System.out.println("You are" + full_name);
                       ^
StringVariables2.java:18: error: ')' expected
        System.out.println("You are" + full_name);
                                ^
StringVariables2.java:18: error: ';' expected
            System.out.println("You are" + full_name);
                                  ^
StringVariables2.java:18: error: illegal start of type
        System.out.println("You are" + full_name);
                                            ^
StringVariables2.java:18: error: <identifier> expected
        System.out.println("You are" + full_name);
                                             ^
StringVariables2.java:18: error: ';' expected
        System.out.println("You are" + full_name);
                                              ^
StringVariables2.java:20: error: reached end of file while parsing
}
 ^
16 errors
vonvic@BSW-Computer:~/Java_Applications$ 

Here is the code:

这是代码:

import java.util.Scanner;

public class StringVariables2 {

    public static class main(String[] args) {

    Scanner user_input = new Scanner( System.in );

    String first_name;
    System.out.println("Please enter your first name")
    first_name = user_input.next();

        String first_name;
        System.out.println("Please enter your family name")
        first_name = user_input.next()

        String full_name;
        full_name = first_name +" "+ family_name;

        System.out.println("You are" + full_name);
    }
}

I'm using Ubuntu 13.10 Saucy Salamander.

我正在使用 Ubuntu 13.10 Saucy Salamander。



Now I get this:

现在我明白了:

vonvic@BSW-Computer:~/Java_Applications$ javac StringVariables2.java

StringVariables2.java:13: error: variable first_name is already defined in method     main(String[])
    String first_name;
           ^
StringVariables2.java:18: error: cannot find symbol
    full_name = first_name + " " + family_name;
                                   ^
  symbol:   variable family_name
  location: class StringVariables2
2 errors

回答by d.moncada

Looks like you forgot to add semi-colons to the end of some of your statements:

看起来您忘记在某些语句的末尾添加分号:

System.out.println("Please enter your first name");

System.out.println("Please enter your family name");
first_name = user_input.next();

Edit:

编辑:

Your mainshould also be a method (not a class)

main也应该是一个方法(不是一个类)

   public static void main(String[] args) 

回答by Patricia Shanahan

You need to declare main as a method, not a class:

您需要将 main 声明为方法,而不是类:

public static void main(String[] args)

Declaring main as a class put several executable statements at the top level inside that class, where only declarations are permitted.

将 main 声明为一个类,在该类的顶层放置了几个可执行语句,其中只允许声明。