【发布时间】:2017-04-10 17:55:13
【问题描述】:
我的任务是创建银行的模拟。到目前为止,我取得的进展是生成随机的 Client 对象,制作 4 个 Teller 对象,将 Client 对象放入循环队列中,如果有的话,将 Client 放入空的 Teller。
我现在遇到的问题是,即使设置的条件仍然为真,我所做的while循环也没有继续,它只能执行5次。程序仍然运行。
这是我到目前为止的代码(假设所有变量都已声明):
public void run() {
int counter = 1;
populateTellers(windowArray);
while (counter < 10) {
newClient = generateClient();
System.out.println(newClient.toString() + " arrived at :" + counter);
System.out.println();
clientQueue.enqueue(newClient);
clientList.add(newClient);
System.out.println("The Queue now contains " + clientQueue.numberOfOccupiedCells() + " Client(s):");
for (int x = 0; x < clientList.size(); x++) {
System.out.println(clientList.get(x).toString());
}
System.out.println();
arrayIndex = getATeller(windowArray);
if (arrayIndex != -1) {
clientList.remove(0);
clientQueue.dequeue();
windowArray[arrayIndex].setClient(newClient);
windowArray[arrayIndex].setServiceTime(newClient.getDuration());
System.out.println(windowArray[arrayIndex].getName() + " now serves " + newClient.toString());
} else {
for (int x = 0; x < clientList.size(); x++) {
if (windowArrray[arrayIndex].getServiceTime() == counter) {
windowArray[arrayIndex].setClient(new Client());
System.out.println(windowArray[arrayIndex].getName() + " ends service for " + newClient.toString());
}
}
}
counter++;
}
}
我无法理解的一件事是,当我从 aI = getATeller(wA); ' 中删除代码时,直到它下面的 if 语句的右括号,程序才能工作。我尝试将块放入一个方法中,更改 if(){} 和 else{} 中代码的位置并将条件更改为if(aI==1),但仍然没有任何改进。知道我做错了什么吗?
编辑:所以我已经能够将其缩小到一行代码 windowArray[arrayIndex].setServiceTime(newClient.getDuration());,每当我为柜员的服务时间分配一个值时,问题就会发生,我检查了我的柜员类并在那里那里没有错误。有什么想法可以解决这个问题。
【问题讨论】:
-
循环不会削减它。使用线程和并行。
-
你知道Java支持超过2个字符的变量名,对吧?因此,您可以给它们起有意义的名称,并使您的代码易于理解。我的建议是重构代码,然后使用调试器单步调试代码并弄清楚会发生什么。
-
这是线程的代码吗?
-
如果这是一个线程,你应该知道线程在修改共享状态时可能带来的困难。
-
@SergioMontoro 不,这不是一个线程。
标签: java while-loop