好吧。所以我试图初始化一个对象数组。 ID 是数组的位置。但每次我运行这个时,它总是采用最后输入的 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 时,它都会给我最后输入的 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;
}
提前感谢您的帮助:)
最佳答案
读完你的代码和评论后,我想我明白了这个问题。您似乎不明白 static
的含义,所以让我简单介绍一下:
如果变量或方法是静态
,则意味着[所述变量]不属于由该[所述类]创建的任何对象。
因此,例如:
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,因为TestNum
是一个静态
变量。它不是 test1
对象或 test2
对象的变量。它是类Test
的一个变量。这有道理吗?
因此,在您的代码中,您会重复更改静态变量,并且每当您引用任何 Person
对象时,由于所有变量/方法都是静态
,因此它会引用以下变量类。这就是为什么它看起来只引用最近创建的 Person
对象。
要解决此问题,您只需删除方法前面的 static
即可。一开始似乎很难理解方法/变量何时应该是静态
,但随着时间的推移,随着您的工作和经验的增加,它开始变得越来越有意义。
如果我是你,我也会通读 this 更深入、更 Eloquent 解释。
关于java - 对象初始化数组,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26199950/