Я изучаю язык Java, и я пытаюсь сделать игру MasterMind. я получаю следующее сообщение об ошибке при попытке компиляции, и при попытке отладки кода, я не могу найти ошибку:IndexOutOfBounds, когда он не должен
Exception in thread "main" java.lang.IndexOutOfBoundsException: Index: 97, Size: 4
at java.util.ArrayList.rangeCheck(ArrayList.java:635)
at java.util.ArrayList.remove(ArrayList.java:474)
at mastermind.Combinacion.aciertos(Combinacion.java:115)
at mastermind.Combinacion.principal(Combinacion.java:36)
at mastermind.MasterMind.main(MasterMind.java:21)
Java Result: 1
Следующий код является метод «aciertos», что данный секретный ключ, и сочетание, говорит вам, сколько ваших писем находится в секретном ключе.
public int aciertos(Combinacion comb){
int aciert = 0;
List<Character> clave = new ArrayList();
// Transform the key to a List, so we can easily check what contains and remove elements
for(char c : this.codigo){
clave.add(c);
}
// We go though the user try, and we check if the letter it is in the secret key list. If it is, we remove it from the list, to avoid counting twice if the user has a letter repeated and it is only once in the secret key.
for (int i = 0; i < comb.codigo.length; i++) {
if(clave.contains(comb.codigo[i])){
aciert++;
clave.remove(comb.codigo[i]);
}
}
return aciert;
}
Этих поля из класса Combinacion:
//Size of each combination
private int tamano = 4;
//Los valores válidos son: blanco, negro, azul, rojo, verde, marron
private static Character[] valoresValidos = {'b', 'n', 'a', 'r', 'v', 'm'};
private static List<Character> valores = Arrays.asList(valoresValidos);
private char[] codigo = new char[tamano];
P.D: Я должен начать писать и комментирует все на английском языке, извините за испанские слова.
Что такое строка 115 'Combinacion.java' –
Каков тип' codigo'? 'Символ []'? – rgettman
У вас есть два поля, называемых 'tamano' в классе' Combinacion'? – RealSkeptic