我正在尝试通过这个网站学习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$
这是代码:
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);
}
}
我正在使用 Ubuntu 13.10 Saucy Salamander。
<小时/>现在我明白了:
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
最佳答案
看起来您忘记在某些语句的末尾添加分号:
System.out.println("Please enter your first name");
System.out.println("Please enter your family name");
first_name = user_input.next();
编辑:
你的main
也应该是一个方法(而不是一个类)
public static void main(String[] args)
关于java - 错误 : <identifier> expected,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20182723/