2014-10-02 51 views
1

剛開始使用DrJava,當我嘗試運行我的代碼時,出現非法類文字錯誤。我的代碼編譯,直到運行時纔出現問題。當我在交互終端中運行'java Percolation(5)'時,即使只是以下導致錯誤。「非法類文字」是什麼意思?

public class Percolation { 
    public Percolation(int N) { 
     System.out.println(N); 
    }  
} 

我在Google上找不到任何東西;任何想法可能會發生在這裏?我覺得我失去了一些愚蠢的東西。謝謝!

+0

打電話給你的程序你有一個'main'方法? – khelwood 2014-10-02 07:14:29

+3

實際上'java percolation(5)'永遠不會起作用,因爲你不能有一個叫做'Percolation(5)'的java類。 – khelwood 2014-10-02 07:16:11

回答

1

調用它來運行你的程序,你必須將起始或主方法。這種方法是public static void,它必須採取VARARGS或陣你的論點:

public static void main (String[] args) { 
    // in here, you can do stuff like calling other functions, or creating objects 

    // no checks, just demo how to use the args: 
    Perlocation p = new Perlocation(Integer.parseInt(args[0])); 
} 

然後你就可以通過

java Percolation 5 
+0

儘管如此,Integer#解碼還是很小心的。它與前導零做有趣的事情。我會使用Integer#valueOf或Integer#parseInt – Thilo 2014-10-02 07:40:18

+0

@Thilo感謝那個提示,不知道那個! :) – ifloop 2014-10-02 07:53:34

+0

太棒了,謝謝。在過去,我使用BlueJ來開設課程,這些課程打破了我的想法,並且我猜想隱藏了主要方法。因此,我從來沒有遇到過一個主要的方法。謝謝。 – jtimmins 2014-10-02 16:12:09