我正在尝试使用此网站学习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
答案 0 :(得分:5)
您似乎忘了在一些陈述的末尾添加分号:
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)
答案 1 :(得分:2)
您需要将main声明为方法,而不是类:
public static void main(String[] args)
将main声明为一个类,在该类的顶层放置了几个可执行语句,其中只允许声明。