0

I have a problem with one of 30-days-of-code tasks.

"The Utopian Tree goes through 2 cycles of growth every year. Each spring, it doubles in height. Each summer, its height increases by 1 meter.

Laura plants a Utopian Tree sapling with a height of 1 meter at the onset of spring. How tall will her tree be after growth cycles?"

Here's my solution:

public static void main(String[] args) {
    int h;
    Scanner scanner = new Scanner(System.in);
    int T = scanner.nextInt();
    for (int i=0; i<T; i++){
        int n = scanner.nextInt();
        if (n == 0){
            h = n * 0 + 1;
        } else if (n % 2 == 0) {
                h = (int)Math.pow(2,(n/2)+1)-1;
        } else if (2 % n == 1){

                h = (int)Math.pow(2, (n-1)/2+2)-2;
        }
        System.out.println(h);
   }
}

I have error on sout:

"Variable h may not have been initialized"

When I'm initializing with int h=0 at beginning, then my resault at the sout is always 0.

Community
  • 1
  • 1
Paweł
  • 387
  • 1
  • 3
  • 13

0 Answers0