Я кодирования зомби заражать людей в городе, тогда как: 2: Там нет человека 1: Неинфицированные люди 0: ZombiesРекурсивные функции StackOverflowError
зомби заражает все нормальные люди, которые вокруг зомби. Ниже представлена моя Java-программа, но я получаю сообщение об ошибке: StackOverflowError.
public class InfectGame {
/**
* @param args
*/
public static void main(String[] args) {
// TODO Auto-generated method stub
int a[][] = { { 1, 1, 1, 1, 2, 2, 2, 1, 1, 0 },
{ 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 2, 1, 1, 2, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 1, 0, 1, 1, 1, 1 },
{ 1, 1, 1, 1, 0, 1, 1, 1, 2, 1 },
{ 2, 1, 1, 1, 1, 1, 1, 1, 1, 1 },
{ 2, 2, 2, 1, 1, 1, 1, 1, 1, 2 }, };
int i = 0;
int j = 0;
for (i = 0; i < 10; i++) {
for (j = 0; j < 10; j++) {
if (a[i][j] == 0) {
run_test(i, j, a, 0, 10);
}
}
}
i = 0;
j = 0;
for (i = 0; i < 10; i++) {
System.out.print("\n");
for (j = 0; j < 10; j++) {
System.out.print(a[i][j] + " ");
}
}
}
public static void run_test(int x, int y, int a[][], int v, int size) {
if ((x < 0) || (x >= size))
return;
if ((y < 0) || (y >= size))
return;
// System.out.print(a[x][y] + " ");
// a[x][y] = v;
if (a[x][y] != 2) {
a[x][y] = v;
if (x + 1 < size) {
run_test(x + 1, y, a, v, size);
}
if (x > 0) {
run_test(x - 1, y, a, v, size);
}
if (y + 1 < size) {
run_test(x, y + 1, a, v, size);
}
if (y > 0) {
run_test(x, y - 1, a, v, size);
}
}
}
}
Exception in thread "main" java.lang.StackOverflowError
at InfectGame.run_test(InfectGame.java:55)
at InfectGame.run_test(InfectGame.java:58)
at InfectGame.run_test(InfectGame.java:55)
at InfectGame.run_test(InfectGame.java:58)
at InfectGame.run_test(InfectGame.java:55)
at InfectGame.run_test(InfectGame.java:58)
at InfectGame.run_test(InfectGame.java:55)
at InfectGame.run_test(InfectGame.java:58)
at InfectGame.run_test(InfectGame.java:55)
at InfectGame.run_test(InfectGame.java:58)
at InfectGame.run_test(InfectGame.java:55)
at InfectGame.run_test(InfectGame.java:58)
at InfectGame.run_test(InfectGame.java:55)
at InfectGame.run_test(InfectGame.java:58)
at InfectGame.run_test(InfectGame.java:55)
at InfectGame.run_test(InfectGame.java:58)
at InfectGame.run_test(InfectGame.java:55)
at InfectGame.run_test(InfectGame.java:58)
at InfectGame.run_test(InfectGame.java:55)
at InfectGame.run_test(InfectGame.java:58)
at InfectGame.run_test(InfectGame.java:55)
at InfectGame.run_test(InfectGame.java:58)
........................................... .............
Подробно опишите эту ошибку? –
вы не «возвращаете», когда называете свои рекурсивные методы, это то, что вы намереваетесь? –
Возможный дубликат [Что такое StackOverflowError?] (Http://stackoverflow.com/questions/214741/what-is-a-stackoverflowerror) – Raedwald