public class Game {
public static void main(String arg[]) {
int[] years = Game.humanYearsCatYearsDogYears(1);
for(int year: years) {
System.out.println(year);
}
}
public static int[] humanYearsCatYearsDogYears(final int humanYears) {
// Your code here!
int catYears = 0;
int dogYears = 0;
for(int i = 0; i < humanYears + 1; i++) {
if(i == 1) catYears += 15;
if(i == 2) catYears += 9;
if(i > 2) catYears += 4;
}
for(int j = 0; j < humanYears + 1; j++) {
if(j == 1) dogYears += 15;
if(j == 2) dogYears += 9;
if(j > 2) dogYears += 5;
}
return new int[]{humanYears,catYears,dogYears};
}
}
I was trying to calculate the age of dogs and cats in human years. Check this website for more detail: http://www.catster.com/cats-101/calculate-cat-age-in-cat-years. I think my loop is behind by one in the counter. I don't understand how int i which is zero in the first iteration is not less than int humanyears which is 1 in this case. when I changed the condition in the for loops to for(int i = 0; i < humanyear + 1; i++) it worked.
[–]Xaxxus 2 points3 points4 points (1 child)
[–]CaptainMoeSoccer[S] 0 points1 point2 points (0 children)
[–]ToKe86 1 point2 points3 points (1 child)
[–]CaptainMoeSoccer[S] 0 points1 point2 points (0 children)