0
我在一個學校項目工作,我想創建一個程序,將顯示一個2 x 2的數字網格(從1 - 16隨機生成)。然後這個程序將逐步整理出來。如何使用方法來檢查我想添加到數組中的新元素是否已添加?
我應該有一個邏輯檢查類。
我想做一個方法,它將採取main()程序想要添加的值,檢查它是否已經在數組中,並返回false,如果該數字已經存在或返回如果不是,則爲true
class LogicStatementsV2 {
//Declare constants
private final int LENGTH_OF_INDEX = 16;
//Create an array of how the index of the array should look like
int[] finalIndex = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15};
//Creates an array of length 16 which will be filled as the main program uses a random number generator.
int[] buildIndex = new int[LENGTH_OF_INDEX];
/**Method that is called to add a random number if that number hasnt been entered
* @param n The integer that the main class will generate from a random number generator
* @return wether that number is already in the array or not
*/
public boolean addIfValid(int n) {
for (int i = 0; i < 16; i++) {
if (n == buildIndex[i]) return false;
else return true;
}
}
}
出於某種原因,第一個持續}保持說需要返回值。
我在做什麼錯?