2013-11-24 97 views
2

我想学习java与本网站:http://www.homeandlearn.co.uk/java/user_input.html 但是当我通过终端运行在Ubuntu的代码中,我得到这些错误:爪哇 - 错误:<identifier>预计

[email protected]:~/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 
[email protected]:~/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。


现在我得到这样的:

[email protected]:~/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

使用完整的IDE。不要在文本编辑器中编写代码。 –

+0

您缺少一行一行的分号。 – Sajmon

+0

Ubuntu与这个问题完全无关。 –

回答

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) 
+0

+1这是一些语法错误的原因。 –

2

您需要声明主要作为一种方法,而不是一类:

声明主要为一类把几个可执行语句在那个阶级,只有声明是允许里面的顶尖水平。