我试图通过这个网站学习java: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发布于 2013-11-25 00:01:34
看来你忘了在你的一些陈述的结尾加上分号了:
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) 发布于 2013-11-25 00:04:35
您需要将main声明为方法,而不是类:
public static void main(String[] args)将main声明为类在该类中的顶层放置了几个可执行语句,其中只允许声明。
https://stackoverflow.com/questions/20182723
复制相似问题