java - 在 do while 循环中获取要从队列中删除的项目

标签 java queue do-while

 public class Queue {

  public static void main(String[] args) {

     Queue<Integer> myQueue = new LinkedList<Integer>();
     Random generator  = new Random();

     int totalTime = 60;
     int lineSize=0;
     int serviceTime;
     int currentPatronTimeLeft=50;
     int timeKeeper;
     int newPatron; 
     int totalCheckedOut=0;



     do
     {
        //for new customer 25%of the time
        serviceTime = generator.nextInt(5) + 1;
        newPatron = generator.nextInt(4) + 1;
     //nested if else to control the adding and removing of items based on service time and the 25% stat 
        if(newPatron == 1)
        {
           myQueue.add( new Integer(serviceTime));
           System.out.println("a new customer has been added to line");
           lineSize++;
           System.out.println("The Queue size is now: "+ lineSize);
           currentPatronTimeLeft = myQueue.peek();

        }
        else if(currentPatronTimeLeft == 0 && lineSize != 0)
        {
           myQueue.poll();
           lineSize--;   
           System.out.println("A customer has been removed from the queue");
           totalCheckedOut++;

           System.out.println("The Queue size is now: "+ lineSize);
        }
        else if (lineSize != 0)
        {
           currentPatronTimeLeft =myQueue.peek();
        }


        currentPatronTimeLeft--;
        totalTime--;
        System.out.println();
        System.out.println("---------------------------------------------------");

     }while(totalTime != 0);





     System.out.println();

  }

}

我无法让我的程序在人员应该完成其服务时间后将其从队列中删除。任何帮助将不胜感激。程序产生的输出有点像这样

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 1

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 2

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 3

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 4

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 5

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 6

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 7

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 8

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 9

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 10

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 11

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 12

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 13

---------------------------------------------------
a new customer has been added to line
The Queue size is now: 14

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

---------------------------------------------------

最佳答案

从代码中删除以下条件:

else if(lineSize != 0)
 {
           currentPatronTimeLeft =myQueue.peek();
 }

应该可以正常工作

关于java - 在 do while 循环中获取要从队列中删除的项目,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14997859/

相关文章:

c - C 中 do while 循环条件错误

JAVA:嵌套的 do-while 循环如何丢弃输入缓冲区中的字符?

java - 如何用java写一个小数

Android N 中使用 jsoup 下载数据时出现 java.lang.VerifyError

java - 将 void 相互放入时出错(编译器无法编写代码)

php - Laravel 不使用数据库执行作业队列

java - javascript 中是否有等效的 Vector Java 类?

Python:更新heapq中元素的值

c# - 为什么 Stack<T> 和 Queue<T> 用数组实现?

c - 交错开关上的模糊 C 构造