Reference no: EM132075891
You need help figuring out how to match my inputed hour value with an array of set values. I can make everything work from there. Here is the problem You are looking at, and below that is code thus far. Any pointers or suggestions would be greatly appreciated.
Problem
You think rabbits breed quickly? Tribbles are worse by far! Dr. McCoy of the Enterprise is trying to determine how many tribbles are there going to be every hour.
Now, he suspects that they are born pregnant with one baby-tribble (which saves a lot of time when you think about it) that is born within the first hour. His empirical research has identified these amount of tribbles alive at each hour:
2 2 2 6 10 18 34 62 114 210
Using this sequence, write a JAVA program that uses recursion to determine the number of tribble for any hour. It should prompt the user for the hour and then display the total tribbles.
For example, the user enters 1 and the program displays 2; the user enters 10 and the program displays 210; and so on.
Code
import java.util.Scanner;
public class DBtest {
//private static int[] anArray;
public static void main(String[] args){
Scanner keyboard = new Scanner(System.in);
System.out.print("Enter the number of hours: ");
int hours = keyboard.nextInt();
System.out.println(num(hours));
}
public static int num(int hours){
int[] anArray = new int[hours];
//anArray[1] = 2;
//anArray[2] = 2;
//anArray[3] = 2;
if (hours <= 3)
return 2;
else
return num(hours - 1) + num(hours - 2);
}
}