2013-11-24 101 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

您需要聲明主要作爲一種方法,而不是一類:

聲明主要爲一類把幾個可執行語句在那個階級,只有聲明是允許裏面的頂尖水平。