2014-09-04 166 views
-6

我無法獲得公共代碼構造函數的正確語法。因此,這是主要的:正確的語法

public class Main_code { 

    public static void main(String[] args) { 

     Code C = new Code; 
     C.code(); 
     C.make(); 
    } 
} 

這是市民代碼:

public class code { 

    int x; 
    int numbers; 
    int [] result; 

這是構造:

public code(){ 

    this.x=0; 
    this.numbers [] = {6, 3, 8, 9, 1, 4, 7}; 
    this.result= new int[x]; 
} 

最後,這是無效:

public void make(){ 

     for (int i=0; i<numbers.lenght){ 
      if(this.numbers[i]<5){ 
       this.result [x] = this.numbers[i] 
      } 
     } 
     for (int e=0; e<this.result.length;e++){ 
      System.out.println (this.result[e]); 
     } 
    } 
+0

向我們顯示您收到的錯誤消息。告訴我們你的想法是什麼意思。 – pamphlet 2014-09-04 21:30:24

+0

「大寫的C代碼應該是小寫的」代碼「嗎?因爲它們是不同的標識符 – khelwood 2014-09-04 21:34:15

回答

1
this.numbers [] = {6, 3, 8, 9, 1, 4, 7}; 

是無效的,numbersint類型,而不是一個數組

,即使它是一個數組,你就去做

this.numbers = new int[] {6, 3, 8, 9, 1, 4, 7}; 
0

您創建一個新的實例時忘記括號。而不是Code c = new Code;Code c = new Code(); //<-added brackets。此外,Java區分大小寫,因此您必須將類和構造函數的名稱更改爲Code,否則java將不會識別Main_code類中的構造函數。你應該閱讀通過java namig conventions,只是爲了一個更清潔的代碼。