mardi 5 janvier 2016

Java Recursion in an If/Else statement

I'm trying to figure out why the top block of Code executes but the bottom block of code does not. All I did to get the other to execute was switch the conditions position in my if/else statement

public static void onTheWall(int bottles){
    if (bottles == 0){
        System.out.println("No bottles of beer on the wall,"
                           + " no bottles of beer, ya’ can’t take one down, ya’ can’t pass it around," 
                           + "cause there are no more bottles of beer on the wall!");
    } else if (bottles <= 99){
        System.out.println(bottles + " bottles of beer on the wall, " 
                           + bottles + " bottles of beer, ya’ take one"
                           + " down, ya’ pass it around, " 
                           + (bottles - 1) + " bottles of beer on the wall");
        onTheWall(bottles-1);
    }
}

public static void onTheWall(int bottles){
    if (bottles <= 99){
        System.out.println(bottles + " bottles of beer on the wall, " 
                           + bottles + " bottles of beer, ya’ take one"
                           + " down, ya’ pass it around, " + (bottles - 1) 
                           + " bottles of beer on the wall");
        onTheWall(bottles-1);
    } else if (bottles == 0){
        System.out.println("No bottles of beer on the wall," 
                           + " no bottles of beer, ya’ can’t take one down, ya’ can’t pass it around," 
                           + "cause there are no more bottles of beer on the wall!");
    }
}

Aucun commentaire:

Enregistrer un commentaire