Question: I have this code that takes a dogs age and calculates its age in dog years. I would like to add to it that you

I have this code that takes a dogs age and calculates its age in dog years. I would like to add to it that you can enter your dogs name and the output would include the name. For example, you input 7 and your dogs name is Lucy. It would print "Lucy's age in human years is: 49"
Code:
import java.util.Scanner;
public class MyDog {
public static void main(String[] args){
Scanner scanner = new Scanner(System.in);
//user is prompted to enter their dogs age
System.out.print("Enter your dog's age: ");
int dogYears = scanner.nextInt();
int humanYears;
//if-else branch to determine the output of your dogs age
if (dogYears <0){
//dogs age under 0 is not a valid age
System.out.println("Invalid age.");
return;
} else if (dogYears ==0){
//dogs age in human years cannot be calculated until at least 1 year old
System.out.println("Your dogs age in human years cannot be calculated until it as at least one year old.");
return;
} else if (dogYears ==1){
//the first year of a dogs life is equal to 15 human years
humanYears =15;
} else if (dogYears ==2){
//the second year of a dogs life is equal to 9 human years (15+9)
humanYears =24;
} else {
//every year after the second year is equal to 5 human years
humanYears =24+(dogYears -2)*5;
}
//output prints the users dogs age in human years
System.out.println("Your dog's age in human years is: "+ humanYears);
}
}

Step by Step Solution

There are 3 Steps involved in it

1 Expert Approved Answer
Step: 1 Unlock blur-text-image
Question Has Been Solved by an Expert!

Get step-by-step solutions from verified subject matter experts

Step: 2 Unlock
Step: 3 Unlock

Students Have Also Explored These Related Finance Questions!