What is the best way to prevent the same numbers from being entered when entering?

public int fiveNumbers = 1; public String[] nForBingo = new String[5]; public int[] randomNumbersBingo = new int[70]; public boolean win = false; public int i = 0; public int chooseNumber = 0; public int j = 0; public boolean checkCreatedBingo = false; public int numbersInBrackets = 0; public int allNumbers = 0; public int option; public void run() { while (!win) { BINGO(); } } public int BINGO() { System.out.println( "\n************************ * BINGO ************************\n" + "* 1) Create a Bingo card\n" + "* 2) Draw a number\n" + "* 3) Check Bingo card\n" + "* 9) Exit \n*********************************************************"); if (option == 9) { win = true; return -1; } System.out.print("Select a menu option: "); option = scan.nextInt(); if (option == 1) { return createBingo(); } else if (option == 2) { return drawNumber(); } else if (option == 3) { return checkBingoCard(); } else if (option == 9) { win = true; return -1; } return BINGO(); } public int createBingo() { for (; i < nForBingo.length; ) { System.out.print("Enter a value for field " + fiveNumbers + ": "); nForBingo[i] = scan.next(); if (chooseNumber > 20) { System.out.println("The entered value is too high! All values must be between 0 and 20."); return createBingo(); } else if (chooseNumber < 0) { System.out.println("The entered value is too low! All values must be between 0 and 20."); return createBingo(); } else { i++; fiveNumbers++; } } checkCreatedBingo = true; return BINGO(); } public int drawNumber() { if (!checkCreatedBingo) { System.out.println("You must first create a Bingo card before drawing a number!"); return BINGO(); } else { randomNumbersBingo[j] = ((int) (Math.random() * 20)); System.out.println("A number is drawn! The ball reads the number: " + randomNumbersBingo[j] + "!"); System.out.print("["); for (i = 0; i < nForBingo.length; i++) { if (nForBingo[i].equals(String.valueOf(randomNumbersBingo[j]))) { numbersInBrackets++; nForBingo[i] = "(" + nForBingo[i] + ")"; } System.out.print(nForBingo[i]); if (i < 4) { System.out.print(", "); } } System.out.println("]"); allNumbers++; if (numbersInBrackets == 5) { System.out.println("BINGO! All numbers are marked in " + allNumbers + " turns."); option = 9; return BINGO(); } return BINGO(); } } public int checkBingoCard() { if (checkCreatedBingo) { System.out.print("Your Bingo cart: [ "); for (int j = 0; j < 5; j++) { System.out.print(randomNumbersBingo[j]); if (j < 4) { System.out.print(", "); } } System.out.println(" ]"); if (numbersInBrackets != 4) { System.out.println("Unfortunately you don't have Bingo card yet!"); } else if (numbersInBrackets == 4) { System.out.println("BINGO! All numbers are marked in " + allNumbers + " turns."); } }else { System.out.println("You must first create a Bingo card!"); } return BINGO(); } 
  • how best to do it so that you can’t enter the same digits when wired - Oleh Kozub

2 answers 2

You will need to store the entered numbers in some structure. The most effective way is to use the Set (set) from the Java Collections API. In a nutshell, the Set interface stores unique elements and allows you to perform add / remove / check for existence extremely quickly (depending on the implementation). Examples:

 //Создаём новое множество и инициализируем стартовыми значениями. //Т. к. множество хранит лишь уникальные значения, в set будут содержаться {1, 2, 3, 4, 5} Set<Integer> set = new HashSet<>(Arrays.asList(1, 2, 3, 4, 5, 2)); System.out.println(set.contains(3));//Проверяем, содержиться ли значение в множестве System.out.println(set.contains(6)); 

From the advantages of this solution: ease of use and speed. Of the minuses: a large overhead memory.

    You can create an array with values ​​that you have already entered. And before entering a new check: is there such a number in the array. If there is, then look for a new number. Otherwise, put the value in our array and continue the logic of the program.

    • Set is more rational - user31238