我試圖檢查一個加油泵是否可以免費使用& &滿了氣,然後我試着讓那個加油泵讓汽車在排隊時使用。爲什麼我的if語句不起作用?
Thread carThreads[]=new Thread[TOTAL_CARS];
try {
Pump pump1 = new Pump();
pump1.setName("pump1");
pump1.setFuelAmount(2000);
pump1.setState(0);
Pump pump2 = new Pump();
pump2.setName("pump2");
pump2.setFuelAmount(2500);
pump2.setState(0);
Pump chosenPump = new Pump();
if(pump1.getState()==0 && pump1.getFuelAmount()<0){
chosenPump = pump1;
System.out.println("Pump1 is free and has a fuel amount of: "
+ (pump1.getFuelAmount()));
}
else if (pump2.getState()==0 && pump2.getFuelAmount()<0){
chosenPump = pump2;
System.out.println("Pump2 is free and has a fuel amount of: "
+ (pump2.getFuelAmount()));
}
//else{
// System.out.println("Must wait for the tanker. It should be here soon");
//}
Random r = new Random();
Car car;
for(int i = 0; i<TOTAL_CARS; i++){
car = new Car(i, chosenPump);
System.out.println("car" + car.getID() + " was created");
(carThreads[i] = new Thread(car)).start();
Thread.currentThread().sleep(r.nextInt(10000));
line.enqueue(car);
chosenPump.usePump((Car)line.getfirst(), chosenPump, line);
System.out.println("this is the new line size for gas: " + line.size());
}//end for
}//end try
catch (Exception e){
}
}//end of main
如果聲明不起作用?有沒有錯誤?我們需要更多細節。 – 2010-10-29 18:58:32