我的程序是Sprocket製造公司的倉庫庫存系統。鏈輪具有定向屬性(0-359度,一個int)。有2種類型的鏈輪:口袋(順時針旋轉)和Nockets(逆時針旋轉),有3種變化:紅色(轉動5度),木製(轉動10度)和鈦(轉動20度)。我有Sprocket,Nocket,Pocket,RedNocket,RedPocket,WoodenNocket,WoodenPocket,TitaniumNocket和TitaniumPocket。Java倉庫庫存程序:turn()方法錯誤地改變屬性
我有一個VirtualWarehouse對象,它包含一個可調整大小的Sprocket對象數組。我可以通過鍵盤輸入向倉庫添加Sprockets,用戶可以在其中輸入他們希望添加的Sprockets的類型,變體和數量。我需要一種方法,根據它們是什麼類型的Sprocket(例如:WoodenPocket將順時針旋轉10度),將所有Sprocket對象轉換爲適當的度數。我的問題是,無論何時「添加」給定數量的Sprockets,然後告訴程序打開所有Sprocket,程序每次都會在每次Sprocket循環時都會變成Sprocket。例如,如果我添加了三個WoodenPocket,則所有WoodenPocket對象的方向都會提前10(因爲它應該是)三次(因爲添加了三個項目)。我不明白爲什麼會發生這種情況,所以我完全無法修復它。我的代碼的重要部分如下。
public class Main {
public static void main(String[] args) {
...
switch(userInput.toUpperCase()){
...
case "ADD":
Sprocket newSprocket = getSprocketTypeToAdd();
System.out.print("How many of this type of sprocket would you like to
add: ");
if (input.hasNextInt()) {
numOfSprockets = input.nextInt();
} else {
System.out.println("Error. You must enter an integer.");
}
myWarehouse.add(newSprocket, numOfSprockets);
doMore = checkMoreInput();
validInput = true;
break;
case "TURN":
if (myWarehouse.isEmpty()) {
System.out.print("Error: warehouse is empty. ");
} else {
myWarehouse.turnAll();
}
doMore = checkMoreInput();//checks if user wants to continue
validInput = true;//ends loop that catches invalid input
break;
...
}//end switch
...
}//end main method
}//end Main class
public class VirtualWarehouse {
...
//properties
private Sprocket[] warehouseContents;
private int numberOfEntries;
...
//(constructors, getters/setters, other methods)
...
public boolean add(Sprocket newEntry, int quantity) {
int counter = 0;
for (int i = 0; i < quantity; i++) {
ensureCapacity(); //resizes array as necessary
warehouseContents[numberOfEntries] = newEntry;
numberOfEntries++;
counter++;
}
return (counter == quantity);
}//end add method
...
public void turnAll() {
for (int i = 0; i < numberOfEntries; i++) {
warehouseContents[i].turn();
}
}//end turnAll method
...
}//end VirtualWarehouse class
public class WoodenPocket extends Pocket {
...
@Override // overrides empty methods in Sprocket and Pocket
public void turn() {
if (getOrientation() > 359) {//modulate orientation so it remains between 0-359
setOrientation(getOrientation() - 360 + 10);
} else {
setOrientation(getOrientation() + 10);
}
}//end turn method
...
}//end WoodenPocket class
如果我和4個WoodenPockets跑一圈()方法,在倉庫的0的起始取向,其結果將是具有40我猜測,有一個問題的取向每WoodenPocket對象屬性與調用方法相關的方式,但我不明白是什麼。
我從來沒有遇到過這樣的事情(這是我的第二個學期),並且我沒有成功嘗試過各種方法來欺騙代碼進行工作(使對象成爲轉向參數,使轉向返回需要轉動的度數等)。我還用調試器檢查了代碼,我只是更加迷茫。
順便說一句,這是一項家庭作業。我真的只是想明白爲什麼代碼正在做它正在做的事情。
謝謝!
編輯: 我改變了349爲359. 這裏是我的程序的一些輸入/輸出的複製/粘貼。大括號中的數字是當前鏈輪的方向。
Enter would you like to do (ADD, REMOVE, TURN, RESET, REPORT, SAVE, or LOAD): add
Enter what type of Sprocket you would like to add (POCKET or NOCKET): pocket
Enter what variation of POCKET you would like to add (RED, WOODEN, or TITANIUM): wooden
What are these sprockets' current orientation (between 0-359 degrees): 0
How many of this type of sprocket would you like to add: 5
Would you like to do more (YES or NO): yes
Enter would you like to do (ADD, REMOVE, TURN, RESET, REPORT, SAVE, or LOAD): report
This warehouse contains sprockets with an orientation between:
0-59: 5 { 0 0 0 0 0 }
60-119: 0 { }
120-179: 0 { }
180-239: 0 { }
240-299: 0 { }
300-359: 0 { }
Would you like to do more (YES or NO): yes
Enter would you like to do (ADD, REMOVE, TURN, RESET, REPORT, SAVE, or LOAD): turn
Would you like to do more (YES or NO): yes
Enter would you like to do (ADD, REMOVE, TURN, RESET, REPORT, SAVE, or LOAD): report
This warehouse contains sprockets with an orientation between:
0-59: 5 { 50 50 50 50 50 }
60-119: 0 { }
120-179: 0 { }
180-239: 0 { }
240-299: 0 { }
300-359: 0 { }
你是否使方向值爲靜態? –
不,方向是類Sprocket中的非靜態變量。 – tori
那麼也許你只有幾個副本的同一個對象而不是幾個不同的對象 –