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

Java 在do while循环中获取要从队列中删除的项目,java,queue,do-while,Java,Queue,Do While,您的问题在于代码的这一部分: --------------------------------------------------- --------------------------------------------------- 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: 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

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

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

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

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

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

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

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

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

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

---------------------------------------------------
如果没有人添加到队列,而当前队列尚未完成,则将
currentPatronTimeLeft
更新为当前客户端的开始值。在那之后,你减少它。如果在下一次迭代中,没有新的客户机添加到队列中,那么当前客户机将不会完成,因为他剩余的时间是他从开始到现在的时间-1。然后将
currentCustomerTimeLeft
再次更新为其开始值,然后再将其递减。这样它就永远不会变成0

解决方案:

else if (lineSize != 0)
{
    currentPatronTimeLeft =myQueue.peek();
}
currentPatronTimeLeft--;
else if(CurrentCustomerTimeLeft==0&&lineSize!=0)
{
myQueue.poll();
线条尺寸--;
//插入以下行:
如果(线宽>0){
CurrentCustomerTimeLeft=myQueue.peek()+1;/+1递减后
}
//我的插入结束
System.out.println(“客户已从队列中删除”);
totalCheckedOut++;
System.out.println(“队列大小现在为:“+lineSize”);
}

如果(lineSize!=0)块,则必须去除整个

从代码中删除以下条件:

else if(currentPatronTimeLeft == 0 && lineSize != 0)
{
    myQueue.poll();
    lineSize--;
    // insert following lines:
    if (lineSize > 0) {
        currentPatronTimeLeft =myQueue.peek() + 1; // +1 <--> following decrement
    }
    // end of my insert
    System.out.println("A customer has been removed from the queue");
    totalCheckedOut++;
    System.out.println("The Queue size is now: "+ lineSize);
}

它应该可以正常工作

如果您遇到了问题,那么currentCustomerTimeLeft将始终是第一个元素,因此永远不会达到零。快速修复方法是使用另一个变量来实现这些目的。

你有调试器吗?@RADAI我没有调试器,我们必须经常滚动,因为你的代码有很多空行。在此之前,请考虑把它格式化好。尝试将代码减少到显示问题的绝对最小值-请参见“好的,我已更改:else if”(CurrentPatientTimeLeft==0 | | lineSize!=0),现在我已删除项prematurely@user2094629:使用
| |线宽!=一旦0进入,它将始终被删除。我在上面发布了一个解决方案。好吧,它似乎运行得更好,并且似乎处于正确的模式。输出为新客户已添加到生产线。他们的服务时间是:1队列大小现在是:1---------------------------------------------一个新客户已添加到队列中。他们的服务时间是:3队列大小现在是:2 1-------------------------------------------客户已从队列中删除队列大小现在是:1-------------------------------------------破折号表示1次迭代,但我觉得输出是一次迭代是的,因为现在它是正确的。请不要忘记接受最好的答案,看看它是如何工作的:并向上投票选出有用的答案。您建议如何使用另一个变量来替换currentTimeLeft?或者你是在建议我另外添加一个变量吗?因为我还不能对其他答案发表评论,@user2094270如果currentCustomerTimeLeft=myQueue.poll(),你最好添加第一个变量;
else if(currentPatronTimeLeft == 0 && lineSize != 0)
{
    myQueue.poll();
    lineSize--;
    // insert following lines:
    if (lineSize > 0) {
        currentPatronTimeLeft =myQueue.peek() + 1; // +1 <--> following decrement
    }
    // end of my insert
    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();
 }