-1

キューのシミュレーションを作成しようとしていますが、すべての結果から要求を差し引いた結果の最後に常に 0.0 が返される理由がわかりません。それは私には論理的に理にかなっていますが、waitTime変数の変更で常に自分自身を差し引くことになる理由がよくわかりません.これが私の結果です: Numbers Served 322 Total Wait time 0.0 Average Wait time: 0.0

プログラムを生成して実行するたびに、実際の値が得られないのはなぜだろうと思っていました。言い換えれば、それは問題なくコンパイルされます。私が犯した間違いはわかりません

import java.util.*;


public class IndividualQueues {

final static int simulationTimeLimit = 3600;
final static double arrivalChance = 1.0/10;
final static int numberOfServers = 5;

public static void main(String[] args) {
    Queue queueArray[] = new Queue[numberOfServers];
    int[] serviceTime = new int[numberOfServers];
    Random checkArrival = new Random();
    double totalWaitTime = 0;
    int requests = 0;
    int serviceTimer = 0;
    int waitTime = 0;
    Queue <Integer> tempQueue = new Queue<Integer>();

      for (int i=0;i<numberOfServers;i++){
       queueArray[i]= tempQueue;
      }

   for(int seconds = 1;seconds<= simulationTimeLimit;seconds++) {
       if(seconds <= simulationTimeLimit) {
           if(checkArrival.nextDouble() <= arrivalChance) {
               requests++;
                       serviceTimer = checkArrival.nextInt(49) + 10;
                       int min = queueArray[0].size();
                       for(int i = 1;i<queueArray.length;i++) {
                           int temp = queueArray[i].size();
                           if(min >temp)
                           min = temp;
                       }
                       queueArray[min].enqueue(seconds);
                       serviceTime[min] = serviceTimer;
               }


               for(int j = 0;j<serviceTime.length;j++) {
                   if(serviceTime[j] >0) {
                       serviceTime[j]--;
                     //  System.out.println(serviceTime[j]);
                   }
                   else if(serviceTime[j]== 0 && queueArray[j].size() >0) {
                       System.out.println("You got here");
                       int dequeue = (Integer)queueArray[j].dequeue();
                       waitTime = seconds - dequeue;
                       totalWaitTime += waitTime;
                   }
               }

           }
   }
   System.out.println("Numbers Served " +requests);
   System.out.println("Total wait time " +totalWaitTime);
   System.out.println("Average wait time: " +(double)totalWaitTime/requests);
}

}


public class Queue<Item> {
private int N;         // number of elements on queue
private Node first;    // beginning of queue
private Node last;     // end of queue

// helper linked list class
private class Node {
    private Item item;
    private Node next;
}

/**
  * Create an empty queue.
  */
 public Queue() {
     first = null;
     last  = null;
     N = 0;
 }

/**
  * Is the queue empty?
  */
  public boolean isEmpty() {
      return first == null;
  }

 /**
  * Return the number of items in the queue.
  */
  public int size() {
     return N;
  }

 /**
   * Return the item least recently added to the queue.
   * @throws java.util.NoSuchElementException if queue is empty.
   */
  public Item peek() {
     if (isEmpty()) throw new NoSuchElementException("Queue underflow");
     return first.item;
   }

 /**
   * Add the item to the queue.
   */
  public void enqueue(Item item) {
      Node oldlast = last;
      last = new Node();
      last.item = item;
      last.next = null;
      if (isEmpty()) first = last;
      else           oldlast.next = last;
      N++;
  }

 /**
   * Remove and return the item on the queue least recently added.
   * @throws java.util.NoSuchElementException if queue is empty.
   */
  public Item dequeue() {
      if (isEmpty()) throw new NoSuchElementException("Queue underflow");
      Item item = first.item;
      first = first.next;
      N--;
      if (isEmpty()) last = null;   // to avoid loitering
      return item;
  }    

 /* *
   * Return string representation.
   */
  public String toString() {
      StringBuilder s = new StringBuilder();
      Node current = first;
      while (current != null)
      {
          s.append(current.item + " ");
          current = current.next;
    }
      return s.toString();
  }
  }  
4

1 に答える 1

1

配列内のすべてのキューに同じキューを使用しています

Queue <Integer> tempQueue = new Queue<Integer>();
for (int i=0;i<numberOfServers;i++){
    queueArray[i]= tempQueue;
}

for ループで各キューを作成する必要があります。

for (int i=0;i<numberOfServers;i++){
    //I don't know if this works
    //queueArray[i]= new Queue<Integer>();
    //LinkedList implements the Queue interface
    queueArray[i]= new LinkedList<Integer>();
}
于 2012-10-12T03:50:17.107 に答える