2014-04-24 25 views
0

我不斷收到第44行的「錯誤:無法找到符號」,我無法弄清楚我錯過了什麼符號。我確定我所有的變量都被聲明瞭。有人能幫我在我的代碼中找到問題嗎?Java符號錯誤

class personne{ 
    private String naissance; 
    private int nbCafe; 

    public personne(String year, int number){ 
     naissance=year; 
     nbCafe=number; 
    } 
    public personne(String year){ 
     naissance=year; 
     nbCafe=1; 
    } 

    public String getnaissance(){ 
     return naissance; 
    } 
    public int getnbCafe(){ 
     return nbCafe; 
    } 
    public void afficher(String message){ 
     System.out.println(message+ ": nee le 16 novembre 1994, consomme 2 tasse(s) de cafe"); 
    } 

    public void affichertable(personne [] table, int amount,String message){ 
     System.out.printf("Contenu du tableau de %d personne(s) %s", amount,message); 
      System.out.printf("Naissance  nbCafe"); 
     for (int i=0; i<amount;i++) 
      System.out.printf("%6.2s %8.2d\n", table[i].getnaissance(), table[i].getnbCafe()); 


    } 
} 

public class popo{ 
    public static void main(String args[]){ 
     personne p1= new personne("16/11/1994",2); 
     personne p2=new personne("15/12/1990"); 

     p1.afficher("Informations de p1"); 

     personne[] pers={ new personne("12/10/1991",3),new personne("15/10/1990",6), new personne("13/07/1993",3), new personne("05/06/1991"),new personne("16/12/1992",3)};  
     int nbpers=pers.length; 

     affichertable(pers,nbpers,"premier tableau");//This is line 44 where the error occurs 
    } 
} 
+0

錯誤消息告訴你哪個符號。在這種情況下,這就是'affichertable',它沒有在'popo'中聲明。 –

+0

並請尊重命名約定。 –

+0

但是這種方法已經在課堂上聲明瞭嗎?而即時通訊基本上只是調用它在主? – user3268216

回答

3

affichertablepersonne一個實例方法。您試圖將其稱爲popo中的靜態方法。

您應該打電話p1.affirchertable(...)p2.affichertable(...)

另外,如果affirchertable方法不意味着依賴的personne單一實例的狀態,你應該將其更改爲一個靜態方法,並調用它爲:

personne.affichertable(...); 

(順便說一句,我會強烈建議你遵循普通的Java命名約定,並大寫你的類名 - 並在不同的源文件中放入不同的類。)

+0

thx我試過personne.affichertable,它似乎工作,但打印表格有一些困難「錯誤:非靜態方法」。是的,我的老師告訴我如果程序員會恨我,如果我不把我的班級名稱變成大寫字母,什麼都不會。 – user3268216

+0

@ user3268216:如果你得到一個錯誤,那麼它就是* not *工作 - 你需要使它成爲一個靜態方法(正如我在我的回答中所描述的)。 –

+0

是的,我做到了,但它不斷顯示這個奇怪的長文本:線程「主」異常java.util.IllegalFormatPrecisionException:2 \t at java.util.Formatter $ FormatSpecifier.checkInteger(Formatter.java:2936) \t at java .util.Formatter $ FormatSpecifier。 (Formatter.java:2684) \t在java.util.Formatter.parse(Formatter.java:2528) \t在java.util.Formatter.format(Formatter.java:2469) \t在java.io.PrintStream中.format(PrintStream.java:970) \t在java.io.PrintStream.printf(PrintStream.java:871) \t在popo.affichertable(popo.java:34) \t在popo.demo2(popo.java: 44) \t at popo.main(popo.java:54) – user3268216