2014-10-05 133 views
0

好的。所以我試圖初始化一個對象數組。 ID是陣列的位置。但是每次我運行它時,它總是會將當前輸入的最後一個ID(不管我輸入什麼ID,它都返回輸入的最後一個ID)。因此,這裏的全碼:對象初始化數組

public class Main { 
private static String answerString = "Start"; 
private static final double TPS = 0.05; 
private static final double TVQ = 0.09975; 
private static final int BILLET = 800; 
private static double prixTotal; 
public static int persNum; 
private static int siege1 = 35; 
private static int siege0 = 1; 
private static double prixBilletAv, prixTPS, prixTVQ, prixBilletAp; 
private static int answerInt; 
public static Scanner scan = new Scanner(System.in); 
public static Person[] pers = new Person[9999]; 


public static void main(String[] args) { 

    System.out.println("Bienvenue à bord de SonicXpress!"); 
    System.out.println(""); 
    System.out.println("Loading. . ."); 

    for(int i = 0; i < 9999; i++){ 
     System.out.println("yo"); 
     pers[i] = new Person(); 
    } 

    System.out.println("Done loading!"); 
    System.out.println(""); 
    while(!"Exit".equals(answerString)) 
     begin(); 
    } 
public static void begin(){ 
    System.out.println("Que veux-tu faire? (Creer , Check , Total , Exit) : "); 
    answerString = scan.next(); 

    if("Creer".equalsIgnoreCase(answerString)){ 
     createPers(); 
    }else{ 
     if("Check".equalsIgnoreCase(answerString)){ 
      checkPers(); 
     }else{ 
      if("Exit".equalsIgnoreCase(answerString)){ 
       System.out.println("Exiting"); 
       System.exit(0); 
      }else{ 
       if("Total".equalsIgnoreCase(answerString)){ 
        total(); 
       }else{ 
        System.err.println("Bad value!"); 
        System.exit(0); 
       }  
      } 
     } 
    } 
} 
public static void createPers(){ 

    System.out.print("Entrez le numéro du client(ID) :\n"); 
    answerInt = scan.nextInt(); 

    persNum = answerInt; 
    pers[persNum].setID(answerInt); 

    System.out.print("Entrez le nom du client(name) : \n"); 
    answerString = scan.next(); 

    pers[persNum].setName(answerString); 

    System.out.print("Entrez l'age du client : \n"); 
    answerInt = scan.nextInt(); 

    pers[persNum].setAge(answerInt); 

    System.out.print("Entrez la classe (0 = première classe et 1 = classe touriste): \n"); 
    answerInt = scan.nextInt(); 

    pers[persNum].setClasse(answerInt); 
    if(pers[persNum].getClasse() == 1){ 
     pers[persNum].setSiege(siege1); 
     siege1 ++; 
    }else{ 
     if(pers[persNum].getClasse() == 0){ 
      pers[persNum].setSiege(siege0); 
      siege0++; 
     } 
    } 
} 

public static void checkPers(){ 
    System.out.print("Entrez le numéro du client ID : \n"); 
    answerInt = scan.nextInt(); 
    prixBilletAv = BILLET + 10; 
    System.out.println(answerInt); 
    persNum = answerInt; 
    System.out.println(persNum); 
    System.out.println("--- Réservation : "); 
    System.out.println("Le nom du client est : " + pers[answerInt].getName()); 
    System.out.println("L'age du client est : " + pers[answerInt].getAge()); 
    System.out.println("La classe du client est : " + pers[answerInt].getClasse()); 
    System.out.println("Le numéro de siège du client est : " + pers[answerInt].getSiege()); 
     if (pers[persNum].getAge() < 12 || pers[answerInt].getAge() >= 60){ 
      prixBilletAv = BILLET - 50; 
     } 
     if (pers[persNum].getClasse() == 0){ 
      prixBilletAv = prixBilletAv + 100; 
     } 
     prixTVQ = prixBilletAv * TVQ ; 
     prixBilletAp = prixBilletAv + prixTVQ; 

     prixTPS = prixBilletAv * TPS; 
     prixBilletAp = prixBilletAp + prixTPS; 

     pers[persNum].setPrixBillet(prixBilletAp); 

    System.out.println("Le cout du billet est : " + prixBilletAv); 
    System.out.println("TPS : " + prixTPS); 
    System.out.println("TVQ : " + prixTVQ); 
    System.out.println("Montant à payer : " + pers[persNum].getPrixBillet()); 
} 

public static void total(){ 
    for (int i = 0; i < 9999; i++){ 
     prixTotal = prixTotal + pers[i].getPrixBillet(); 
    } 
    System.out.print("Montant total des billets vendus = " + prixTotal + "\n"); 
    System.out.println("-------------------------------------------------------------------------------------"); 
} 

的問題是,每當我試圖檢查特定的ID它給了我進入了最後一個。對於爲例,如果我set Id = 1270name = Joeage = 43等爲對象1和ID = 1824name = Bobage = 24 for object 2, when I check, it will automatically output object 2 no matter what ID`我把

而對於第二類:

class Person { 
private static int ID; 
private static String name; 
private static int age; 
private static int classe; 
private static double prixBillet; 
private static int siege; 

/** 
* @return the ID 
*/ 
public static int getID() { 
    return ID; 
} 

/** 
* @param aID the ID to set 
*/ 
public static void setID(int aID) { 
    ID = aID; 
} 

/** 
* @return the name 
*/ 
public static String getName() { 
    return name; 
} 

/** 
* @param aName the name to set 
*/ 
public static void setName(String aName) { 
    name = aName; 
} 

/** 
* @return the age 
*/ 
public static int getAge() { 
    return age; 
} 

/** 
* @param aAge the age to set 
*/ 
public static void setAge(int aAge) { 
    age = aAge; 
} 

/** 
* @return the classe 
*/ 
public static int getClasse() { 
    return classe; 
} 

/** 
* @param aClasse the classe to set 
*/ 
public static void setClasse(int aClasse) { 
    classe = aClasse; 
} 

/** 
* @return the prixBillet 
*/ 
public static double getPrixBillet() { 
    return prixBillet; 
} 

/** 
* @param aPrixBillet the prixBillet to set 
*/ 
public static void setPrixBillet(double aPrixBillet) { 
    prixBillet = aPrixBillet; 
} 

/** 
* @return the siege 
*/ 
public static int getSiege() { 
    return siege; 
} 

/** 
* @param aSiege the siege to set 
*/ 
public static void setSiege(int aSiege) { 
    siege = aSiege; 
} 

THX在您的幫助:)

+0

你的init代碼看起來不完整,但是有什麼好的和非常基本的 - 它只是把對象放入數組中。如果你發佈更多的代碼,它可能會更好...... – Krease 2014-10-05 05:37:12

+0

我也有點困惑。當你說它給你輸入的最後一個你的意思是:如果你輸入5作爲變量「answerString」它會返回5爲personID? – 2014-10-05 05:39:15

+0

把setID()方法的代碼 – 2014-10-05 05:39:54

回答

1

通過您的代碼和您的意見看完後,我想我明白這個問題。您好像不明白static的意思,所以讓我簡單介紹一下:

如果一個變量或方法是static,這意味着[所述變量]不是由[this class]創建的ANY對象的一部分。 所以,例如:

public class Test{ 
    public static int TestNum = 0; 

    public static void setTestNum(int newNum){ 
     TestNum = newNum; 
    } 

    public static int getTestNum(){ 
     return TestNum; 
    } 

    public static void main(String args[]){ 
     Test test1 = new Test(); 
     test1.setTestNum(5); 

     Test test2 = new Test(); 
     System.out.println(test2.TestNum); 
    } 
} 

上面的例子輸出5,因爲TestNumstatic變量。它不是test1對象或test2對象的變量。它是class Test的變量。這有意義嗎?

因此,在您的代碼中,您將重複更改靜態變量,並且每當您引用任何Person對象時,由於所有變量/方法都是static,因此它會引用該類的變量。這就是爲什麼它只是引用最近創建的Person對象。

要解決這個問題,您必須簡單地擺脫方法前面的static。首先要明白方法/變量何時應該是static似乎有點困難,但隨着時間的推移,當你工作並變得更有經驗時,它開始變得越來越有意義。

如果我是你,我還會通過this閱讀更深入和有說服力的解釋。

0

變化

private static int ID; 

private int ID; 

如果變量爲static,則表示它屬於該類。也就是說,無論您創建多少個對象,堆上的某個地方只有一個id。您每次修改id的內容,最後一次修改是保留的內容。如下圖所示

public void setID(int aID) { 
    this.ID = aID; 
} 

getID()

public int getID() { 
    return this.ID; 
} 

另外

變化setID,正確的語法從非靜止象素塊內訪問任何static構件或方法是

className.staticMemberVariable 
className.staticMethodName 

如果從訪問塊,你不必與classname

例如有資格的static字段或方法的名稱,如果id是靜態

public static void main (String...a) { 
    //here just say id 
    id = 5; 
} 
+0

Nope沒有工作它仍然給我編輯的最後一個對象 – Lacni 2014-10-05 06:19:42

+1

好吧,所以我刪除了所有的getters和setters靜態的東西,它的工作! – Lacni 2014-10-05 06:31:48