好的。所以我試圖初始化一個對象數組。 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 = 1270
,name = Joe
,age = 43
等爲對象1和ID = 1824
,name = Bob
,age = 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在您的幫助:)
你的init代碼看起來不完整,但是有什麼好的和非常基本的 - 它只是把對象放入數組中。如果你發佈更多的代碼,它可能會更好...... – Krease 2014-10-05 05:37:12
我也有點困惑。當你說它給你輸入的最後一個你的意思是:如果你輸入5作爲變量「answerString」它會返回5爲personID? – 2014-10-05 05:39:15
把setID()方法的代碼 – 2014-10-05 05:39:54