我正在尝试检查加油泵是否可以免费使用&&充满汽油,然后我尝试使该泵成为队列中的汽车使用的泵。
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
最佳答案
您正在检查pump1.getFuelAmount()<0
这将检查泵的燃油量是否为负。如果你想查看它的燃油量是否为正,你需要执行pump1.getFuelAmount()>0
和pump2.getFuelAmount()>0
关于java - 为什么我的 if 语句不起作用?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4054967/