0
 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();

  }

}

serviceTimeを完了した後で、プログラムでキューからユーザーを削除するのに問題があります。どんな助けでも大歓迎です。プログラムはこのような出力を生成します

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

---------------------------------------------------
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

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

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

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

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

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

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

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

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

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

---------------------------------------------------
4

3 に答える 3

2

コードから次の条件を削除します。

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

正常に動作するはずです

于 2013-02-21T08:56:41.803 に答える
0

あなたの問題はコードのこの部分にあります:

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

誰もキューに追加されず、現在のキューが終了していないcurrentPatronTimeLeft場合は、現在のクライアントの開始値に更新します。その後、それをデクリメントします。次の反復で新しいクライアントがキューに追加されない場合、残りの時間は最初からの時間であるため、現在のクライアントは終了しません-1。次に、currentPatronTimeLeftデクリメントする前に、開始値に再度更新します。このようにして、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);
}

そして、あなたelse if (lineSize != 0)ブロック全体を取り除く必要があります。

于 2013-02-21T08:56:07.903 に答える
0

if-elseで問題が発生した場合、currentPatronTimeLeftが常に最初の要素になるため、ゼロに達することはありません。簡単な修正は、これらの目的の1つに別の変数を使用することです。

于 2013-02-21T08:59:40.070 に答える