![]() |
![]() |
![]() |
![]() |
![]() |
|
The version of the rabbits method shown in the program in
Segment 452 contains an ifelse statement:
public class Demonstrate {
public static int rabbits (int n) {
if (n == 0 || n == 1) {
return 1;
}
else {
return rabbits(n - 1) + rabbits(n - 2);
}
}
}
If you wish, you can rewrite the program without the || operator,
handling separately cases of 0 or 1 rabbit:
public class Demonstrate {
public static int rabbits (int n) {
if (n == 0) {return 1;}
else if (n == 1) {return 1;}
else {return rabbits(n - 1) + rabbits(n - 2);}
}
}