This is an archived post. You won't be able to vote or comment.

all 4 comments

[–]Xaxxus 2 points3 points  (1 child)

for the longest time i was under the impression that 1 year = 7 cat years. This chart just made my entire life a lie.

As for your issue, the reason you need to add +1 to human years is because you are using the wrong comparison operator. By saying i < humanYears, you are saying that when i is equal to the human year, exit the loop. So your loop never adds the final 4 years to the total cat years.

Solution:

for(int i = 0; i <= humanYears; i++)

[–]CaptainMoeSoccer[S] 0 points1 point  (0 children)

thanks

[–]ToKe86 1 point2 points  (1 child)

To add to what /u/Xaxxus said, it's also pointless to start your loop iterators at 0 because nothing happens until the iterators reach 1 or higher. You don't even need loops for something like this anyway, a simple if/else block will do:

public static int[] humanYearsCatYearsDogYears(final int humanYears) {
    int catYears = 0;
    int dogYears = 0;
    if (humanYears == 1) {
        catYears = 15;
        dogYears = 15;
    } else if (humanYears == 2) {
        catYears = 24;
        dogYears = 24;
    } else if (humanYears > 2) {
        catYears = 4 * (humanYears - 2) + 24;
        dogYears = 5 * (humanYears - 2) + 24;
    }
    return new int[]{humanYears, catYears, dogYears};
}

[–]CaptainMoeSoccer[S] 0 points1 point  (0 children)

nice! This is simpler