0
嘗試以隨機間隔將對象添加到ArrayDeque。這是我java以隨機間隔將對象添加到ArrayDeque
for (int i = 0; i <= 100; i ++) {
if (window.isEmpty()) {
Customer customer = new Customer(r.nextInt(10)+1);
q.add(customer);
window.beginService();
}
else {
Customer customer = new Customer(r.nextInt(10)+1);
q.add(customer);
window.beginService();
totalCustomers++;
totalServiceTime += window.serviceTime;
totalWaitTime += customer.getArrivalTime();
}
}
正在使用的其他方法
public boolean isEmpty() {
if (serviceTime == 0) {
return true;
}
else
return false;
}
public void beginService() {
if (isEmpty()) {
serviceTime = r.nextInt(10)+1;
}
else
serviceTime += r.nextInt(10)+1;
}
public Customer(int arrivalTime) {
this.arrivalTime = arrivalTime;
}
public int getArrivalTime() {
return arrivalTime;
}
當我打印出我的客戶數就等於我的時鐘時間,這不應該是爲客戶隨意添加間隔1-10。有任何想法嗎?
居然弄明白了,別人有類似的問題,幫我解決了我的問題。一石二鳥http://stackoverflow.com/questions/6639243/java-add-items-to-arraylist-at-random-time-intervals/6643169#6643169 – fernan