我是一名普通程序员,试图编制交通信号灯。我有我所有的代码,但我的摇摆计时器给我问题。我希望所有的灯泡倒数到零。在这些顺序,红色第一,琥珀色下一个,绿色最后。我希望它这样做十次但是当我在一个循环中运行时,我不断让计时器独立运行。我现在想要的是倒计时应该在另一个开始之前完成。循环显示在gui开始之前完成。如果可能的话,请帮我替代或更正。感谢期待!
// setting values that each bulb uses to count down
private int red_time = 10;
private int amb_time = 3;
private int grn_time = 10;
private int speed = 500; // 0.5 second
Queue myqueue = new LinkedList();
Queue dupqueue = new LinkedList(); //this queue contains same elements as myqueue. used to get next signal
public int cars1;
public int cars2;
public int cars3;
public int cars4;
Timer timer;
int counter = 0;
/**
* Creates new form bulbs
*/
public bulbs() {
initComponents();
red1.setText(null);
amb1.setText(null);
grn1.setText(null);
red2.setText(null);
amb2.setText(null);
grn2.setText(null);
// the first view sees only the red bulb on
red1.setEnabled(true);
amb1.setEnabled(false);
grn1.setEnabled(false);
/* my algorithm
1. Load ← [ L1,L2,L3,L4]
2. RESULT_QUEUE ← COMPARE (LOAD)
3. NEXT_SIGNAL ← ADD(RESULT_QUEUE)
4. COUNTER ← 0
5. WHILE (!EMPTY(NEXT_SIGNAL && COUNTER MOD != 10)):
a. PREDICTION_LIST ←ADD(LOAD)
b. MOVE ← POP(NEXT_SIGNAL)
c. RELEASE (MOVE, LOAD) UNTIL TIMER ==0
d. NEXT_SIGNAL ← ADD(MOVE)
e. LOAD ← GENERATE(LOAD)
f. COUNTER++
6. RESULT_QUEUE ← PREDICT(LOAD)
7. NEXT_SIGNAL ← ADD (RESULT_QUEUE)
8. GOTO 4
9. EXIT
*/
Car car = new Car();
Compare com = new Compare();
Store store = new Store();
//prediction list for all lanes
int [] pred1 = new int [10];
int [] pred2 = new int [10];
int [] pred3 = new int [10];
int [] pred4 = new int [10];
// put cars on each lane
car.put_car(1);
car.put_car(2);
car.put_car(3);
car.put_car(4);
cars1 = car.lane1cars;
cars2 = car.lane2cars;
cars3 = car.lane3cars;
cars4 = car.lane4cars;
// setting the cars on lanes
lab1.setText(String.valueOf("CARS: "+car.lane1cars));
lab2.setText(String.valueOf("CARS: "+car.lane2cars));
lab3.setText(String.valueOf("CARS: "+car.lane3cars));
lab4.setText(String.valueOf("CARS: "+car.lane4cars));
int next_signal;
while ( counter <10 && !myqueue.isEmpty()){ // the problem area i have
//collecting useful loads for prediction
pred1[counter] = car.lane1cars;
pred2[counter] = car.lane2cars;
pred3[counter] = car.lane3cars;
pred4[counter] = car.lane4cars;
//collect next signal
next_signal = ((Integer)myqueue.remove());
System.out.println(next_signal);//this console prints completely before GUI starts running
start(next_signal);
myqueue.add(next_signal);
counter++;
}
}
以下方法与上述方法在同一类中:
private void start(final int lane){
ActionListener action = new ActionListener(){
public void actionPerformed(ActionEvent evt){
if (red_time >= 0){
red1.setText(String.valueOf(red_time));
red_time --;
}
else if (amb_time >= 0){
amb1.setEnabled(true);
red1.setEnabled(false);
red1.setText(null);
amb1.setText(String.valueOf(amb_time));
amb_time -- ;
}
else if (grn_time >= 0){
System.out.println();
grn1.setEnabled(true);
amb1.setEnabled(false);
amb1.setText(null);
grn1.setText(String.valueOf(grn_time));
grn_time -- ;
}
else{
timer.stop();
grn1.setText(null);
grn1.setEnabled(false);
red1.setEnabled(true);
reset_time();
System.out.println (red_time);
}
}
};
timer = new Timer(speed,action);
timer.setInitialDelay(0);
timer.start();
}