K
K
kvaks2019-12-03 17:17:27
Java
kvaks, 2019-12-03 17:17:27

Why does recursion in java go to infinity?

I'm just learning in java, and I decided to make a sapper game, this is a function to check the number of mines around, it has recursion, individually each if works, but pairs of X or Y fall into errors.

static void getNumMine(int x, int y){

      if (!check(x,y)){
          int number = 0;
   
          if (x<9)      if (check(x+1,y)) number++; else getNumMine(x+1,y);
          if (y<9)      if (check(x,y+1)) number++; else getNumMine(x,y+1);
          if (x>0)      if (check(x-1,y)) number++; else getNumMine(x-1,y);
          if (y>0)      if (check(x,y-1)) number++; else getNumMine(x,y-1);

          main.box.setSum(x,y,number);

      }
    }

Answer the question

In order to leave comments, you need to log in

1 answer(s)
V
Vamp, 2019-12-04
@kvaks

An ideal task for step-by-step debugging. It is in any decent IDE. I'll use IDEA as an example:
1. Put a so-called breakpoint on the line you want to debug.
2. Run the program in debug mode.
3. The program will start to run as usual, but when the execution reaches the line marked with a breakpoint, the execution will stop and the debug window will display the current stack trace, the values ​​of local variables and, most importantly for your problem, the step-by-step continue buttons.
These are5de7ecb0c3d98348194703.png
The first button will advance the program execution by one line. The second one will do the same, but if some method is called in the line being executed, then debugging will step inside the method and the steps will continue in the body of the called method.
By debugging step by step, you will accurately imagine what is happening inside the program and you will understand where the legs of the bug grow from. The sooner you learn step-by-step debugging, the easier it will be in the future.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question