我正在尝试为队列编写模拟,尽管我不明白为什么我总是在最后得到 0.0 减去请求的所有结果。这对我来说合乎逻辑,但我不太明白为什么它总是在 waitTime 变量修改中减去自己这是我的结果:服务数字 322 总等待时间 0.0 平均等待时间: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();
}
}