In java, I have defined a first array of characters, char[]
, with a maximum of 4 elements, and a second array of characters that is filled by user input with a maximum of 4 elements.
I want to compare the first element (index[0]) of the first array with each of the elements of the other array, and so on with each element of the first array (which is compared with each of the elements of the other array)
I know I can't use the == operator since it compares references, and it's the same as the equals() method.
The exercise dictates that I can only use loops or the Arrays class and its equals() method, I have tried in various ways applying logic but I left it up to here, I can't get it, any help?
If matches are found, the program must increment a variable for each match. note: both arrays are of type char
for (int i = 1; i <= MAX_INTENTOS; i++) {
System.out.print("Código " + i + " de 10>> ");
letrasEntrada = entrada.nextLine();
if (i == MAX_INTENTOS) {
perdiste = true;
}
for (int j = 0; j < LARGO_CODIGO; j++) {
letrasAdivinador[j] = letrasEntrada.charAt(j);
}
for (int x = 0; x < letrasPensador.length; x++) {
Arrays.equals();
// if (letrasAdivinador[x] == letrasPensador[x]) {
// b += 2;
// } else {
// if (x + 1 < letrasPensador.length && letrasAdivinador[x] == letrasPensador[x + 1]) {
// b += 1;
// }
// if (x + 2 < letrasPensador.length && letrasAdivinador[x] == letrasPensador[x + 2]) {
// b += 1;
// }
// if (x + 3 < letrasPensador.length && letrasAdivinador[x] == letrasPensador[x + 3]) {
// b += 1;
// }
// }
}