簡體   English   中英

即使條件仍然成立,while循環也不會繼續

[英]While loop doesn't continue even though condition still holds true

我的任務是創建銀行的模擬。 到目前為止,我所取得的進展是生成隨機的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);的一件事是,當我從aI = getATeller(wA);刪除代碼時aI = getATeller(wA); 直到程序下面的if語句的右括號為止。 我嘗試將塊放入方法中,更改if(){}和else {}中代碼的位置,並將條件更改為if(aI==1) ,但仍然沒有任何改善。 關於我在做什么錯的任何想法嗎?

編輯:所以我已經能夠將其范圍縮小為windowArray[arrayIndex].setServiceTime(newClient.getDuration()); ,每當我為櫃員的服務時間分配值時,都會出現問題,我已經檢查了櫃員類,並且那里沒有錯誤。 關於如何解決這個問題的任何想法。

為什么只執行5次? 根據代碼, counter用作檢查是否應繼續循環的counter 更改counter的唯一實例是在最后一行: counter++

我可以猜到您在櫃台上做了一個System.out.print ,結果到了5,神奇地停了下來。 它很可能引發異常,該異常不會被捕獲,並且會丟失。

編輯:如果這段代碼正在另一個線程上運行,則可能是它掛在了某個函數上。 按照這里的指示做

嘗試找出循環變成無限循環的地方,

嘗試在必要時編寫打印語句,以查找執行流程。

然后打印出您的變量值,也檢查它們是否與預期的一樣,繼續縮小范圍,因為您發現某個范圍內發生了錯誤,那么您將找到罪魁禍首,如果它導致您使用其他功能,則請徹底檢查該特定值功能。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM